QP School

Full Version: Creating an input box to ask a user for its name in VBA
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Description:

This code prompts the user to enter their name using an input box and then displays a personalized greeting.

Example:

Sub PersonalizedGreeting()
    Dim userName As String

    userName = InputBox("Please enter your name:")
    If userName <> "" Then
        MsgBox "Hello, " & userName & "! Welcome to Excel VBA."
    Else
        MsgBox "You did not enter a name. Goodbye!"
    End If
End Sub

Explanation:

This code defines a subroutine named PersonalizedGreeting. 
It declares a variable userName to store the user's input. 
The InputBox function displays a prompt asking the user to enter their name. 
If the user provides a name, a personalized greeting is shown in a message box. 
If the user leaves the input box empty, a different message is displayed.

The InputBox function is used to prompt the user for input. 
The If statement checks whether the userName variable contains a value. 
If it does, the personalized greeting is shown; otherwise, a message indicating that no name was entered is displayed.