+ Reply to Thread
Results 1 to 13 of 13

Macro to Fill Cells With Random Values From A List

Hybrid View

brent_milne Macro to Fill Cells With... 04-28-2022, 02:13 PM
HSV Re: Macro to Fill Cells With... 04-28-2022, 03:38 PM
brent_milne Re: Macro to Fill Cells With... 04-28-2022, 03:47 PM
Artik Re: Macro to Fill Cells With... 04-28-2022, 06:05 PM
Rick Rothstein Re: Macro to Fill Cells With... 04-28-2022, 06:15 PM
Artik Re: Macro to Fill Cells With... 04-28-2022, 06:29 PM
Rick Rothstein Re: Macro to Fill Cells With... 04-28-2022, 07:14 PM
Artik Re: Macro to Fill Cells With... 04-28-2022, 07:32 PM
Rick Rothstein Re: Macro to Fill Cells With... 04-28-2022, 08:25 PM
Artik Re: Macro to Fill Cells With... 04-28-2022, 08:34 PM
brent_milne Re: Macro to Fill Cells With... 04-29-2022, 09:55 AM
HSV Re: Macro to Fill Cells With... 04-29-2022, 02:31 PM
David_Braden Re: Macro to Fill Cells With... 05-18-2022, 07:53 PM
  1. #1
    Forum Contributor
    Join Date
    03-03-2011
    Location
    Ottawa, Ontario
    MS-Off Ver
    365
    Posts
    137

    Macro to Fill Cells With Random Values From A List

    I have a list of values on 1 sheet A2:A14 that I would like to be able to use to generate a new list in a random order in another column. I have played with the Rand function but still haven't figured out a way to do this. Is this possible to do or just wishful thinking? If it is possible I would also like to make sure that no 2 adjacent cells have the same random value. I was hoping to add this macro to a button to generate a new random list whenever it is clicked.

    Thanks for any help.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    868

    Re: Macro to Fill Cells With Random Values From A List

    Hello,

    No guarantee of multiple peers.

    Sub hsv()
     Range("c2:c14") = "=rand()"
     Range("g2:g14") = Application.Index(Range("a2:a14"), [index(rank(c2:c14,c2:c14),)], 1)
     Range("c2:c14").ClearContents
    End Sub
    Harry.

  3. #3
    Forum Contributor
    Join Date
    03-03-2011
    Location
    Ottawa, Ontario
    MS-Off Ver
    365
    Posts
    137

    Re: Macro to Fill Cells With Random Values From A List

    Thanks, this is what I was thinking of but as you mentioned there is the chance of multiples. Is there any way to randomly select a cell and then have that cell never come up again? For example if the rand() returns a3 then remove a3 from the range to be selected for the next rand() call. I know it's a big ask and was thinking that it would probably involve a huge nested if statement.

    Thanks for getting me started in the right direction though.

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,543

    Re: Macro to Fill Cells With Random Values From A List

    Sub hsv_1()
        Range("G2:G14").ClearContents
        Range("H2:H14").FormulaR1C1 = "=IF(RC[-1]=R[1]C[-1],1,0)"
    
        Range("C2:C14").Formula = "=RAND()"
    
        Do Until [SUM(H2:H14)] = 0
            Range("G2:G14") = Application.Index(Range("A2:A14"), [INDEX(RANK(C2:C14,C2:C14),)], 1)
        Loop
    
        Range("C2:C14,H2:H14").ClearContents
    End Sub
    Artik
    Last edited by Artik; 04-29-2022 at 05:01 AM. Reason: Code improvement

  5. #5
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,476

    Re: Macro to Fill Cells With Random Values From A List

    .
    *** IGNORE THIS POST - IT DOES NOT DO WHAT THE OP WANTS ***

    Give this macro a try...
    Sub RandomizeList()
      Dim Cnt As Long, RandIndx As Long, Tmp As Variant, Arr As Variant
      Randomize
      Arr = Range("A2", Cells(Rows.Count, "A").End(xlUp))
      For Cnt = 1 To UBound(Arr)
        RandIndx = Int(Cnt) * Rnd + 1
        Tmp = Arr(RandIndx, 1)
        Arr(RandIndx, 1) = Arr(Cnt, 1)
        Arr(Cnt, 1) = Tmp
      Next
      Range("E2").Resize(UBound(Arr)) = Arr
    End Sub
    Last edited by Rick Rothstein; 04-28-2022 at 08:28 PM.

  6. #6
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,543

    Re: Macro to Fill Cells With Random Values From A List

    Rick, it was also about:
    Quote Originally Posted by brent_milne View Post
    If it is possible I would also like to make sure that no 2 adjacent cells have the same random value.
    Artik

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,476

    Re: Macro to Fill Cells With Random Values From A List

    Quote Originally Posted by Artik View Post
    Rick, it was also about:
    The code I posted will never have duplicate values anywhere in the outputted list.

  8. #8
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,543

    Re: Macro to Fill Cells With Random Values From A List

    I understood the task in such a way that the same values cannot occur next to each other ( like in the picture).

    Example.png

    Artik

  9. #9
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,476

    Re: Macro to Fill Cells With Random Values From A List

    Quote Originally Posted by Artik View Post
    I understood the task in such a way that the same values cannot occur next to each other ( like in the picture).
    You are right, my code is producing duplicates. The reason? I did not look at the OP's existing data carefully enough to see that there are duplicate values in his original data... I had assumed the existing data was unique. Given that the original data is not unique, my posted code does not and cannot work given that all it does is shuffle the existing data randomly. Thanks for persisting so that I eventually looked back to see my erroneous assumption.

  10. #10
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,543

    Re: Macro to Fill Cells With Random Values From A List

    🥂🥂

    Artik

  11. #11
    Forum Contributor
    Join Date
    03-03-2011
    Location
    Ottawa, Ontario
    MS-Off Ver
    365
    Posts
    137

    Re: Macro to Fill Cells With Random Values From A List

    Thanks for your replies, I took a little bit from both of them and somehow managed to add other pieces to make it work. I did change my original data but thanks again for pointing me in the right direction.

  12. #12
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    868

    Re: Macro to Fill Cells With Random Values From A List

    Or:

    No auxiliary columns.
    Sub hsv_2()
    Dim sv
    sv = Range("a2:a14")
      Do
        Range("a2:a14") = "=rand()"
        Range("a2:a14") = Application.Index(sv, [index(rank(a2:a14,a2:a14),)], 1)
      Loop While [sum(--(a1:a13=a2:a14))] > 0
    End Sub
    Or with one:
    Sub hsv()
     Range("c2:c14") = "=rand()"
      Do
        Range("a2:a14") = Application.Index(Range("a2:a14"), [index(rank(c2:c14,c2:c14),)], 1)
      Loop While [sum(--(a1:a13=a2:a14))] > 0
     Range("c2:c14").ClearContents
    End Sub

  13. #13
    Registered User
    Join Date
    05-18-2022
    Location
    California, United States
    MS-Off Ver
    365
    Posts
    1

    Re: Macro to Fill Cells With Random Values From A List

    Not sure if this gets at your question, but the following functions can be used to either select data, without replacement, from a column of cells (a row if you use TRANSPOSE) or to generate a set of indices:
    Option Explicit

    '-------------------------------
    Function SampleCellsNoReplace(data As Range, ByVal SampleSize As Long) As Variant
    'Returns a sample, without replacement, of Samplesize from a column of cells
    'PLEASE retain all comments:
    'Written and posted 1999/4/1 to microsoft.public.excel.misc by David J Braden
    'Originally posted as "HGSample"

    Dim hiP1 As Long, i As Long, j As Long
    Dim ret() As Variant, temp As Variant

    Application.Volatile 'comment out this line for single shot

    temp = data
    hiP1 = data.Rows.Count + 1
    'If SampleSize > UBound(temp) Then SampleSize = UBound(temp)
    ReDim ret(1 To SampleSize, 1 To 1)
    For i = 1 To SampleSize
    j = i + Int(Rnd * (hiP1 - i))
    ret(i, 1) = temp(j, 1): temp(j, 1) = temp(i, 1)
    Next i
    SampleCellsNoReplace = ret
    End Function


    Function SampleNoReplace(ByVal lo As Long, ByVal hi As Long, ByVal SampleSize As Long) As Variant
    'Returns a sample, without replacement, of SampleSize from the range lo to hi
    'Written 1999/7/7 David J. Braden
    'PLEASE retain all comments: Originally posted to microsoft.public.excel as HGSample2a

    Dim hiP1 As Long, i As Long, j As Long
    Dim ret() As Variant, temp As Variant

    Application.Volatile 'comment out this line for single shot

    'The following allows more flexible specification of the support.
    If lo > hi Then temp = lo: lo = hi - 1: hi = temp Else lo = lo - 1

    ReDim temp(1 To hi - lo)
    For i = hi - lo To 1 Step -1
    temp(i) = i
    Next
    hiP1 = UBound(temp) + 1
    'If SampleSize > UBound(temp) Then SampleSize = UBound(temp)
    ReDim ret(1 To SampleSize, 1 To 1)
    For i = 1 To SampleSize
    j = i + Int(Rnd * (hiP1 - i))
    ret(i, 1) = temp(j) + lo: temp(j) = temp(i)
    Next i
    SampleNoReplace = ret
    End Function

    Hope this helps

+ 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. Pick Random values from the list to run macro (simulating captcha function)
    By bambino86 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-26-2018, 12:00 AM
  2. Replies: 6
    Last Post: 08-07-2018, 09:25 AM
  3. Creating list of random numbers based on values in other cells
    By rossjohnston in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-01-2015, 10:08 AM
  4. [SOLVED] Random fill color macro
    By korndorb in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-08-2013, 07:25 PM
  5. Fill Random Cells With a Specified Color
    By edizzi in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-02-2013, 04:04 AM
  6. macro to fill in empty cells with certain formulas or values in.
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-20-2011, 01:43 AM
  7. Macro to fill cells when a value is selected from Drop Down List
    By TRJJK73 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-17-2009, 05:13 AM

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