| Member with 26 posts. | | Join Date: Jun 2012 Experience: Intermediate | |
Hey Ersin,
The following code will run do what you ask. It is also expandable so if you want to go to 4 macros, just change MaxNumberOfMacros to 4 at the top of the code (also would need to change iterations to 4). Also it allows you to run less then all macro, by setting Iterations to however many macros you want to run (For example, you have 3 macros and you want to run 2 random ones). It prevents the same macro from being run twice. If you want to use more meaningful macro names, you have to replace the for loop at the beginning with a series of MacroList.Add statements and just add strings (and change the MacroToRun string to not add "Macro" at the beginning). Code:
Sub Random()
Dim MacroList As Collection
Set MacroList = New Collection
Dim MaxNumberOfMacros As Integer
Dim Iterations As Integer
Dim i As Integer
MaxNumberOfMacros = 3
Iterations = 3
For i = 1 To MaxNumberOfMacros
MacroList.Add (i)
Next i
Call NextMacro(MacroList, Iterations)
End Sub
Sub NextMacro(MacroList As Collection, Iterations As Integer)
Dim MacroNumber As Integer
Dim MacroToRun As String
MacroNumber = Int(MacroList.Count * Rnd() + 1)
MacroToRun = "Macro" & MacroList.Item(MacroNumber)
Application.Run (MacroToRun)
MacroList.Remove (MacroNumber)
Iterations = Iterations - 1
If Iterations > 0 And MacroList.Count() > 0 Then
Call NextMacro(MacroList, Iterations)
End If
End Sub
Last edited by gathrawnca; 28-Jun-2012 at 09:39 AM..
|