1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Randomizing Sub Running Order

Discussion in 'Business Applications' started by ersin, Jun 28, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. ersin

    ersin Thread Starter

    Joined:
    Jun 28, 2012
    Messages:
    2
    Is it possible randomizing subs so that it doesn't always run in the same order (currently it will run Macro1 then Macro2 then Macro3. I would like to run Macro2, Macro1, Macro3...or in any other random combination)?


    Sub CallAll()
    Call Macro1
    Call Macro2
    Call Macro3
    End Sub

    Sub Macro1()
    MsgBox "Hello A"
    End Sub

    Sub Macro2()
    MsgBox "Hello B"
    End Sub

    Sub Macro3()
    MsgBox "Hello C"
    End Sub

    Thanks in advance,
     
  2. gathrawnca

    gathrawnca

    Joined:
    Jun 25, 2012
    Messages:
    26
    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
    
     
  3. ersin

    ersin Thread Starter

    Joined:
    Jun 28, 2012
    Messages:
    2
    Thank you; this is great!
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1058884