'IF语句
If 逻辑表达式1 Then
代码块1
ElseIf 逻辑表达式2 Then
代码块2
Else
代码块3
End If
'Select(Switch)语句
Select Case 变量
Case Is 条件1
代码块1
Case Is 条件2
代码块2
Case Else
代码块3
End Select
循环语句:
'For-Next循环,step代表Num+2
Dim Num As Integer
For Num = 1 to 5 Step 2
代码块1
Next Num
'跳出循环
Exit For
'Do-While循环,Until代表直到,意为满足条件就不进行循环
Do 逻辑表达式1
代码块1
Loop
Do Until 逻辑表达式2
代码块2
Loop
带参子过程(无返回值)
’子过程(子函数、子程序)
Sub SubTest(AimNum as Integer)
Dim Num As Integer,Total As Integer
For Num= 1 To AimNum
Total = Total + NumNext Num
’中断子程序的执行
Exit Sub
End Sub
'调用示例
Sub test()
SubTest 100
End Sub
自定义函数(有返回值)
Function CubeSum(x As Double, y As Double)
CubeSum = x*x*x +y *y *y
End Function
Sub Test()
Dim Value As Double
Value = CubeSum(1.5,2.5)
'输出消息
MsgBox Value
End Sub
数组
’LBound表示数组的最小的角标’UBound表示数组的最大的角标
Dim MyArray(1 To 4) As Integer
MyArray(1) = 0
For index = LBound(MyArray) To UBound (MyArray)
MsgBox MyArray (index)
Next index
'默认定义,角标从0开始,为0-3
Dim MyArray(4) As Integer
'Option Base 1语句定义角标从1开始而非0
'二维数组
Dim MyArray(1 To 4,1 To 4) As Integer
For RowNo = LBound (MyArray,1) To UBound(MyArray,1)
For ColNo = LBound (MyArray,2) To UBound (MyArray,2)