文章編號: 141691 - 上次校閱: 2006年10月11日 - 版次: 2.3

XL: Visual Basic 範例使用如果和 case 陳述式

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

在應用程式的 Microsoft Visual Basic 有您可以使用來執行條件式測試的兩個陳述式: 一個 If...次要...Else 陳述式或 Select Case 陳述式。如果...次要...Else 陳述式執行一組依據的運算式值的陳述式,並 Select Case 陳述式執行數個陳述式根據運算式的值的其中一個。

其他相關資訊

Microsoft 僅,為了說明提供程式設計範例,不提供任何明示或默示的保證。這包括,但不限於適售性或適合某特定用途之默示擔保責任。本文假設您已熟悉使用我們所示範的程式設計語言以及建立和偵錯程序所使用的工具。Microsoft 技術支援工程師可以協助解釋特定程序的功能,但它們不會修改這些範例以提供附加功能或建構程序,以符合您特定需求。下列範例巨集顯示如何使用 如果...次要...ElseSelect Case 陳述式。

範例巨集使用如果...然後 … else 陳述式

   Sub Using_IF()

      ' Dimension the variable.
      Dim x As Integer
      ' Place a value in x.
      x = Int(Rnd * 100)
      ' Test to see if x less than or equal to 10.
      If x <= 10 Then
         ' Display a message box.
         MsgBox "X is <=10"

      ' Test to see if x less than or equal to 40 and greater than 10.
      ElseIf x <= 40 And x > 10 Then
         MsgBox "X is <=40 and > 10"

      ' Test to see if x less than or equal to 70 and greater than 40.
      ElseIf x <= 70 And x > 40 Then
         MsgBox "X is <=70 and > 40"

      ' Test to see if x less than or equal to 100 and greater than 70.
      ElseIf x <= 100 And x > 70 Then
         MsgBox "X is <= 100 and > 70"

      ' If none of the above tests returned true.
      Else
         MsgBox "X does not fall within the range"
      End If

   End Sub
				

使用 Select Case 陳述式的範例巨集

   Sub Using_Case()

      ' Dimension the variable.
      Dim x As Integer
      ' Place a value in x.
      x = Int(Rnd * 100)

      ' Start the Select Case structure.
      Select Case x

         ' Test to see if x less than or equal to 10.
         Case Is <= 10
            ' Display a message box.
            MsgBox "X is <=10"

         ' Test to see if x less than or equal to 40 and greater than 10.
         Case 11 To 40
            MsgBox "X is <=40 and > 10"

         ' Test to see if x less than or equal to 70 and greater than 40.
         Case 41 To 70
            MsgBox "X is <=70 and > 40"

         ' Test to see if x less than or equal to 100 and greater than 70.
         Case 71 To 100
            MsgBox "X is <= 100 and > 70"

         ' If none of the above tests returned true.
         Case Else
            MsgBox "X does not fall within the range"
      End Select

   End Sub
				

?考

如需控制程式流程的詳細資訊,按一下 [說明] 功能表和型別上的 [解答精靈]:
請告訴我關於條件式的程式設計
"Visual Basic 使用者指南 》 」 版本 5.0,第 7 章 」 控制如何您程式碼執行 」

Microsoft 請按: < Microsoft Excel/Visual 基本程式設計人員指南 」 版本 7.0,第 3 章 < 控制程式流程 」

在 Microsoft Excel 版本 5.0 按一下 [說明] 功能表上的 [內容],按一下 與 Visual Basic 程式設計;,然後按一下 [搜尋] 按鈕,然後鍵入:
if

-或者-

大小寫
取得更多資訊有關取得應用程式的說明與 Visual Basic 請按一下面的文件編號,檢視 「 Microsoft 知識庫 」 中的發行項:
163435? (http://support.microsoft.com/kb/163435/EN-US/ ) VBA: 應用程式的 Visual Basic 程式設計資源

這篇文章中的資訊適用於:
  • Microsoft Excel 95 Standard Edition
  • Microsoft Excel 5.0 Standard Edition
  • Microsoft Excel 5.0c
  • Microsoft Excel 5.0 for Macintosh
  • Microsoft Excel 5.0a for Macintosh
  • Microsoft Excel 97 Standard Edition
  • Microsoft Excel 98 for Macintosh
關鍵字:?
kbmt kbdtacode kbhowto kbprogramming KB141691 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:141691? (http://support.microsoft.com/kb/141691/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。
Retired KB Article依現狀不再更新的知識庫內容免責聲明
本文旨在說明 Microsoft 不再提供支援的產品。因此,本文係依「現狀」提供,不會再更新。