VBA第一课

1.

excel要想正常用宏功能,首先另存为excel启用宏的工作簿.xlsm

            第二在 文件>>选项>>自定义功能区,添加开发工具

              第三在 开发工具>>宏安全性,选择启用所有宏

           第四步   关闭文件再打开

 2.快捷键宏编写在sheet里,第三点讲的按钮宏在模块里

录制宏设置一个快捷键,使单元格变红

选定一个要变红的单元格,点击 开发工具>>录制宏,设置一个快捷键,小写字母就是ctrl + 小写,大写就是CTRL+shift +大写

在开始栏把这个单元格变红,点击 开发工具>>停止录制

选择别的单元格,使用快捷键字体就变红了

3.按钮设定

新建宏

#单元格B2是以单元格A1值为半径的圆的周长

options explicit(强制声明)

Dim #变量设定

const #常量设置

Option Explicit
Dim r, v
Const PI = 3.14

Sub 按钮2_Click()
r = Cells(1, 1)
v = r * PI * 2
Cells(2, 2) = v

End Sub

 4.

#for循环
Sub 按钮6_Click()
Dim i, s
s = Cells(1, 9)
For i = 1 To 5 Step 1 #step 1 可以省略
    Cells(i, 10) = Cells(i, 10) / s
Next i#i 可以省略    

End Sub

5.if分支

#if then   |  if  then

else        |   elseif

end if      |  end if

Sub 按钮7_Click()
Dim score
score = (Cells(6, 2) + Cells(7, 2) + Cells(8, 2)) / 3
Cells(9, 2) = score
If score >= 85 Then
    Cells(10, 2) = "A"
Else
    If score >= 75 Then
    Cells(10, 2) = "B"
    Else
        If score >= 60 Then
            Cells(10, 2) = "C"
        Else
            Cells(10, 2) = "不及格"
        End If
    End If
End If
    
End Sub
Sub 按钮8_Click()
Dim s, i
For i = 2 To 3
    s = (Cells(6, i) + Cells(7, i) + Cells(8, i)) / 3
    Cells(9, i) = s
    If s >= 85 Then
        Cells(10, i) = "A"
        ElseIf s >= 75 Then
        Cells(10, i) = "B"
        ElseIf s >= 60 Then
        Cells(10, i) = "C"
        Else: Cells(10, i) = "不及格"
    End If
Next i
    
End Sub

6.逻辑运算

 If age < 30 And (field = "数学" Or field = "物理") Then

not 优于 and 优先于or

7,

循环与录制宏的联合使用

8,

while

do while condition

    execution

loop 

Sub 按钮1_Click()
Dim i, j
i = 2
Do While Cells(2, i) <> ""
    j = 2
    Do While Cells(j, i) <> ""
             Cells(j, i).Select
            With Selection.Font
            .Color = -16776961
            .TintAndShade = 0
        End With
        j = j + 1
    Loop
i = i + 1

Loop

End Sub
’嵌套语句,双循环,录制宏与while联合使用

单引号用来添加注释

8.1

Sub 按钮3_Click()
Dim i, j
i = 2
Do While Cells(i, 2) <> ""
    For j = 2 To 9
    Cells(i, j) = Cells(i, j) * 0.98
    Next j
    i = i + 1
Loop
End Sub
'while与for嵌套使用

 9.面向对象

 常见的类有 workbook,worksheet,range

下面实现内容:

每个sheet(1:3)里第一列为成绩,第一行第三个格子为名字,在每个sheet第一行第二个格子求和,最后汇总在sheet4里

Option Explicit

Sub Sheet2_按钮1_Click()
Dim w1 As Worksheet
Dim i, s, h, Name, score
Dim w2 As Worksheet
Dim w3 As Worksheet

For i = 1 To Worksheets.Count - 1
    Set w1 = Worksheets(i)
    s = 0
    For h = 1 To w1.UsedRange.Rows.Count
        
        s = s + w1.Cells(h, 1)
        w1.Cells(1, 2) = s
    Next h
Next i

s = 1
Do While s < Worksheets.Count
    Set w2 = Worksheets(s)
    Set w3 = Worksheets(4)
    
    score = w2.Cells(1, 2)
    Name = w2.Cells(1, 3)
    w3.Cells(s, 1) = Name
    w3.Cells(s, 2) = score
    s = s + 1
Loop

End Sub

10,子程序

call 宏name  

 11,函数

Function 所得税(s)
    
    所得税 = s * 0.12

End Function
-----------------------------
Function 性格判断(s)

Dim score, level
score = s '注意要把参数s赋值给score

    If score >= 11 Then
        level = "暴力"
        ElseIf score >= 5 Then
        level = "正常"
        Else
        level = "温柔"
    End If
    
性格判断 = level

End Function

 12

for each i in oneset

Sub 汇总()
Dim i, r, name, k, j
Dim w1 As Worksheet, w2 As Worksheet
Set w1 = Worksheets("汇总")
For i = 3 To 10
name = Cells(i, 2)
    For Each w2 In Worksheets
        If Right(w2.name, 1) = "" Then
                k = 3
                Do While w2.Cells(k, 2) <> ""
            
                    If UCase(Trim(w2.Cells(k, 2))) = UCase(Trim(name)) Then
                        For j = 3 To 5
                            w1.Cells(i, j) = w1.Cells(i, j) + w2.Cells(k, j)
                        Next j
                    End If
                k = k + 1
                Loop
            End If
    Next w2
Next i

End Sub

13

application连接excel函数

w = Application.WorksheetFunction.Max(Range("A1:B4"))
Valar morghulis
原文地址:https://www.cnblogs.com/super-yb/p/11556952.html