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?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
How about
Code:
Sub KellyMort()
   Select Case Int((3 - 1 + 1) * Rnd() + 1)
      Case 1
         Call macro1
      Case 2
         Call macro2
      Case 3
         Call macro3
    End Select
End Sub
 
Upvote 0
If you want a truly random then you should add at the start of the code Fluff posted.
This will change the seed value used by Rnd so it will be truly random

Code:
Sub KellyMort()

Randomize
   Select Case Int((3 - 1 + 1) * Rnd() + 1)
      Case 1
         Call macro1
      Case 2
         Call macro2
      Case 3
         Call macro3
    End Select
End Sub
 
Upvote 0
Rather them writing a IF stamen with many elseifs you can use Select case.
When the random number is one the macro1 is called, when it is 2 macro2 is called.
 
Upvote 0
That's what the link explains ;)
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,017
Members
448,936
Latest member
almerpogi

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