有两种 If 语句,即单行 if 语句和多行 if 语句。在第一个 Then 之后开始另起一行,即可将 If 语句变为多行 If 语句。否则是单行 If 语句。多行 If 语句总是包含 End If,而单行 If 语句不包含。
注意 由于使用行继续符,单行 If 语句不必在单行上。一般情况下,最好使用多行 If 语句,因为它们的布局更清晰。但对于简单的情况,有时使用单行 If 语句。
Rem Single-line If example 1 Rem Same result as multi-line If example 1 If {Employee.Dept} = "Sales" Then _ formula = {Employee.Salary} * 0.06 _ Else _ formula = {Employee.Salary} * 0.04
下例显示不同形式的单行 If 语句:
Rem Single-line If example 2 Dim per As Number, extra As Boolean per = 2 : extra = False 'An example with no Else clause If {Employee.Dept} = "Sales" Then per = 10 'More than 1 statement in the Then or Else part can 'be included by separating them with colons If {Employee.Dept} = "R&D" Then _ per = 5 : extra = True _ Else _ per = 3