Variable in a Message Box


- Written by Puneet

You can also use a variable in a message box. Let’s say you have a variable that stores a specific value in that variable.

Now you can use that variable in the message box to show the value to the user. In the following example, we have used a macro where we have used the for loop and we have a function to generate a random number.


When you run this code, it loops 10 times and then generates a random number and stores that number in the variable. In the next line of code, we used the same variable to show the value in the message box.

Sub generateRandomNumber()

Dim i As Integer, rNumber As Integer
i = 10

    For i = 1 To i
        rNumber = WorksheetFunction.RandBetween(1000, 2000)
        MsgBox rNumber         
    Next i

End Sub
Last Updated: June 22, 2023