+ Reply to Thread
Results 1 to 3 of 3

With selection - randomly call 1 of 4 macros in each cell.

Hybrid View

  1. #1
    Registered User
    Join Date
    01-25-2018
    Location
    london
    MS-Off Ver
    365
    Posts
    43

    With selection - randomly call 1 of 4 macros in each cell.

    Hello,

    I'd really like some help with this because I've got no idea how to deal with the code that randomises anything in VBA.

    I'd like some code that (with the selection) will cycle through cells and randomly call 1 of 4 macros in each cell. Can anyone help with that please? I've honestly got no idea where to start.

  2. #2
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,312

    Re: With selection - randomly call 1 of 4 macros in each cell.

    Possibly...
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim i As Long, x As Double
        For i = 1 To Target.Cells.Count
            x = Int(WorksheetFunction.RandBetween(1, 4))
            Select Case x
                Case Is = 1
                    Call Macro1
                Case Is = 2
                    Call Macro2
                Case Is = 3
                    Call Macro3
                Case Is = 4
                    Call Macro4
            End Select
        Next i
    End Sub

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: With selection - randomly call 1 of 4 macros in each cell.

    The question is reather vague, especially without a sample file (see yellow banner near the top of the page)
    you may start with something similar to:
    Sub test()
    Dim cell As Range
    For Each cell In Selection
      Select Case WorksheetFunction.RandBetween(1, 4)
        Case 1
          Call macro1(cell)
        Case 2
          Call macro2(cell)
        Case 3
          Call macro3(cell)
        Case 4
          Call macro4(cell)
        Case Else
          MsgBox "wow, we are outside of expected scope"
      End Select
    Next cell
    End Sub
    
    Sub macro1(cell As Range)
    cell.Value = 1
    End Sub
    
    Sub macro2(cell As Range)
    cell.Value = 2
    End Sub
    
    Sub macro3(cell As Range)
    cell.Value = 3
    End Sub
    
    Sub macro4(cell As Range)
    cell.Value = 4
    End Sub
    How to test it: select range C5:C8, press Ctrl and select another range like E8:E11 and run this macro (test) you shall see something like (althought numbers will be different, as it is random process) the attached screenshot
    Attached Images Attached Images
    Best Regards,

    Kaper

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Call Different Macros Based on Value in Single Cell
    By aglawrence in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-01-2018, 02:18 PM
  2. Call a userform from a listbox selection
    By seanpears99 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-10-2013, 03:49 AM
  3. Macros and cell selection
    By royhobbs19 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-21-2009, 06:43 PM
  4. Macros randomly failing when file opened
    By bellman101 in forum Excel General
    Replies: 8
    Last Post: 01-12-2009, 06:39 AM
  5. How to call on other macros
    By Mac3188 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-08-2007, 08:59 PM
  6. To Call 2 macros
    By napalm in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-14-2006, 03:09 AM
  7. [SOLVED] randomly selection
    By Shea in forum Excel General
    Replies: 1
    Last Post: 04-03-2006, 11:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1