+ Reply to Thread
Results 1 to 2 of 2

VBA to Randomly Select a Cell

Hybrid View

Jiptastic VBA to Randomly Select a Cell 02-05-2013, 10:45 AM
rcm Re: VBA to Randomly Select a... 02-05-2013, 10:57 AM
  1. #1
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    VBA to Randomly Select a Cell

    I have a list of people that I need to randomly assign to different tasks. Does anyone know how to do this? Here's what I have:

    Sub Random_Selection()
    
    Dim RandomCell As Integer
    
    For RandomCell = 1 To 100
    
    Cells(RandomCell, 1).Copy
    Sheets("Assign Task").Select
    Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
    ActiveSheet.Paste
    
    Sheets("Staff list").Select
    
    Next
    
    
    End Sub
    What I'm missing obviously is that there is no randomness yet.

  2. #2
    Forum Expert
    Join Date
    11-28-2012
    Location
    Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    2,394

    Re: VBA to Randomly Select a Cell

    This routine will pick at random all members of the range between 1 and rmax
    the use of the array sel() is to ensure that the choices are not repeated.

    Sub randomness()
    
    Dim j, r, rmax
    Dim sel(100)
    sel(0) = 1
    Randomize
    rmax = 20 ' maximun number of choices
    
    For i = 1 To rmax
    
    j = 0
    While sel(j) > 0
    j = Int((rmax * Rnd) + 1)
    Wend
    sel(j) = 1
    Cells(j, 1) = i
    
    Next i
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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