For…Next
Sub toUSD()
Dim rate , i
rate = Cells(8,6)
For i = 11 To 20 Step 1
Cells(i,6) = Cells(i,6) / rate
Next i //此处 i 可以省略
End Sub
- Step 1 默认情况下可以省略,其它情况需要写上
- Next i 中的 变量名 i 可以省略,建议写上
- Tab 键 代码层次分明
IF…ELSE
格式1: If 符合条件 Then
执行分支
Else
执行分支
End if
格式2: If 条件1 Then
执行分支
Elseif 条件2 Then
执行分支
Else if 条件3 Then
...
Else
End if
示例语句
Option Explicit
-------------------
Sub evaluate()
Dim score
score = (Cells(4,6) + Cells(5,6) + Cells(6,6)) / 3
Cells(7,6) = score
If score >= 60 Then
Cells(8,6) = "及格"
Else
Cells(8,6) = "不及格"
End Sub
If Score < 60 Then Cells(8,6) = "不及格"
End if
在不需要第二个分支时,可以不写Else子句
关系运算符
< 小于
> 大于
= 等于
<> 不等于
>= 大于等于
<= 小于等于