Escribir un procedimiento Sub

Un procedimiento Sub es una serie de instrucciones de Visual Basic entre las instrucciones Sub y End Sub que realiza acciones pero no devuelve un valor. Un procedimiento Sub puede tomar argumentos, como constantes, variables o expresiones que se pasan por un procedimiento de llamada. Si un procedimiento Sub no tiene argumentos, la instrucción Sub debe incluir un conjunto vacío de paréntesis.

El siguiente procedimiento Sub tiene comentarios que explican cada línea.

' 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

Vea también

Soporte técnico y comentarios

¿Tiene preguntas o comentarios sobre VBA para Office o esta documentación? Vea Soporte técnico y comentarios sobre VBA para Office para obtener ayuda sobre las formas en las que puede recibir soporte técnico y enviar comentarios.