There are times when you may require your macro to request numbers or text from someone using the worksheet. To do this, we can use an inputBox function.
MyNumber = inputBox ("Please enter a number", "Enter Number")
This statement creates an inputBox with the title Enter Number, and the prompt Please enter a number. The user input from the box is assigned to the variable MyNumber.
You can configure the title and prompt to say whatever you wish by changing the text in quotation marks when you type the statement into your macro. The inputBox can be used to get text input from the user as well.
The following macro uses an inputBox to get a number from the user. The number is added to the value in cell A1, and the new number is assigned to cell A1.
You should notice that the variable MyNumber is declared as a variant. This is because a variant type can handle both text strings and numbers almost seamlessly. This is a good choice for this situation as you cannot be certain what input the user will provide. . |