撰寫副程式

Sub程式是由SubEnd Sub語句所括住的一系列 Visual Basic語句,這些語句會執行動作,但不會傳回值。 Sub程式可以接受引數,例如呼叫程式所傳遞的常數、變數表達式。 如果 Sub 程式沒有引數, Sub 語句必須包含一組空的括弧。

下列 Sub 程式有說明每一行的批註。

' Declares a procedure named GetInfo 
' This Sub procedure takes no arguments 
Sub GetInfo() 
' Declares a string variable named answer 
Dim answer As String 
' Assigns the return value of the InputBox function to answer 
answer = InputBox(Prompt:="What is your name?") 
 ' Conditional If...Then...Else statement 
 If answer = Empty Then 
 ' Calls the MsgBox function 
 MsgBox Prompt:="You did not enter a name." 
 Else 
 ' MsgBox function concatenated with the variable answer 
 MsgBox Prompt:="Your name is " & answer 
 ' Ends the If...Then...Else statement 
 End If 
' Ends the Sub procedure 
End Sub

另請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應