Randomly run one out of three macros

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello ,
I am looking at three separate macros right now.

Say :
Code:
macro1 
macro2 
macro3 .

Below is the button to call
Code:
Private Sub CommandButton1_Click 

       'Inside here I want to choose one of those macros at random
 
End Sub

How will I run just one of those three at random?
 
If all the macros are in the same module, and you don't have any parameters to pass, you can do something like this:

Code:
Sub test1()


    Application.Run Mid("Macro1  Macro2   Macro3   ", Int(Rnd() * 3) * 8 + 1, 8)
    
End Sub
List your macro names in the string, and each of them must take up the same length in the string, 8 characters in this example. You can add the Randomize line in front if desired.
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I was trying to do it with choose, but using Call instead of Application.Run :oops:
Code:
Application.Run Choose(Int((3 - 1 + 1) * Rnd() + 1), "macro1", "macro2", "macro3")
 
Upvote 0
Great! !!

The later codes are very interesting.

Clean cut.

I am really learning a lot
 
Upvote 0
I was trying to do it with choose, but using Call instead of Application.Run :oops:
Code:
Application.Run Choose(Int(([COLOR=#ff0000]3 - 1 + 1[/COLOR]) * Rnd() + 1), "macro1", "macro2", "macro3")

Any particular reason you don't just use 3?
 
Upvote 0
Not really, I usually have variables in there & just swapped them out for hard values.
 
Upvote 0

Forum statistics

Threads
1,215,261
Messages
6,123,942
Members
449,134
Latest member
NickWBA

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