randomizing vba

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
I have a code that ends with a message box. I would like to have that message box randomly choose a message each time the code is run instead of saying the same thing. For example, when the code is run, the VBA will choose from message A,B, or C and that code (which will just be a message box) will run.

Do this make sense, and is this doable?

Thanks,
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I have a code that ends with a message box. I would like to have that message box randomly choose a message each time the code is run instead of saying the same thing. For example, when the code is run, the VBA will choose from message A,B, or C and that code (which will just be a message box) will run.
Something like this should work...
Code:
' Put the following code line with your other Dim statements
Dim MSG As Variant
...
...
' Put this code where you want to display your random message
MSG = Array("This is your first message", "This would be the second message", "And here is the third message")
MsgBox MSG(Int((UBound(MSG) - LBound(MSG) + 1) * Rnd))
 
Upvote 0
Like my message box says: "Wow Rick, You Rock! The code is error free!"

Thanks so much!!!:cool:
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,049
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top