Select 陳述式與 If 陳述式類似。但有時用 Select 陳述式撰寫的公式較清楚且不重複。以下範例驗算 {Customer.Fax} 欄位,決定其區碼是否屬於華盛頓州 (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 條件。在上例中為 Left ({Customer.Fax}[1 To 3])。Select 陳述式從第一個 Case 開始,檢查其結果是否符合 Select 條件,如果是,就執行接下來的陳述式,這樣一直進行到下一個 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 字句。例如,在下例中,如果 {movie.NOM} 是 11,則公式會傳回 extreme。
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 迴圈