30 Advanced Excel Tips for 2026

Excel Champs / VBA / Variable in a Message Box
By Puneet Gogia (Microsoft MVP)

Variable in a Message Box

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

Table of Content hide
[thrive_leads id=’70897′]

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.

vba-variable-messagebox

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