Select 语句与 If 语句相似。然而,有时可以使用 Select 语句编写清晰且重复少的公式。下例对 {客户.传真} 字段求值,以确定区号是美国华盛顿州的区号 (206, 360, 509) 还是加拿大不列颠哥伦比亚省的区号 (604, 250):
Rem Select example 1 Select Case Left ({Customer.Fax}, 3) Case "604", "250" formula = "BC" Case "206", "509", "360" formula = "WA" End Select
紧跟在 Select Case 关键字之后的表达式称为 Select 条件。在上例中 Select 条件是 Left ({客户.传真}[1 To 3])。Select 语句试图找到匹配 Select 条件的第一个 Case,然后执行它后面的语句直到找到下一个 Case。
Rem Same effect as Select example 1 Dim areaCode As String areaCode = Left ({Customer.Fax}, 3) If areaCode In Array ("604", "250") Then formula = "BC" ElseIf areaCode In Array ("206", "509", "360") Then formula = "WA" End If
示例
下面的公式将电影获得的奥斯卡提名分为“低”、“中”、“高”或“极高”几类,并在执行过程中显示 Case 标签后的表达式列表的一些可能匹配项。注意可选的 Case Else 子句。如果 Case 表达式列表没有任何一个与前面的 Case 子句匹配,则匹配 Case Else 子句。例如,在下例中,如果 {电影.提名} 为 11,则公式返回“极高”。
Rem Select example 2 Select Case {movie.NOM} Case 1,2,3, Is < 1 Rem Can have multiple statements in the Rem statement blocks formula = "low" Case 4 To 6, 7, 8, 9 formula = "medium" Case 10 formula = "high" Case Else formula = "extreme" End Select
控制结构 | If 语句 | For/Next 循环