Make a randomize group of terms from list

GratefullyDyed

New Member
Joined
Nov 26, 2016
Messages
27
Office Version
  1. 2021
Platform
  1. Windows
Hello,

I am trying to add some products via csv and would benefit from being able to generate a randomized group of 13 terms from a list of tags. Example;

1663904778818.png


What would be the best way to do this?

Thanks for your help!
 

Attachments

  • 1663904590810.png
    1663904590810.png
    16.1 KB · Views: 6

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try with VBA
Alt-F11 to open VBA window, insert module then paste below code.
Hit F5 to run, or create a button on worksheet then assign macro to this code:

VBA Code:
Option Explicit
Sub RandomGroup()
Dim i&, r&, rng, s As String, s1 As String
rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
Randomize
r = Int(Rnd * UBound(rng)) + 1
s = rng(r, 1) & ", "
For i = 2 To 13
    Do
        r = Int(Rnd * UBound(rng)) + 1
        s1 = rng(r, 1) & ", "
    Loop Until InStr(1, ", " & s, ", " & s1) = 0
    s = s & s1
Next
Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = Left(s, Len(s) - 2)
Columns(3).AutoFit
End Sub

RandomGroup.xlsm
ABC
1Term 1
2Term 2Term 2, Term 19, Term 10, Term 18, Term 11, Term 5, Term 6, Term 13, Term 4, Term 9, Term 16, Term 14, Term 12
3Term 3
4Term 4
5Term 5
6Term 6
7Term 7
8Term 8
9Term 9
10Term 10
11Term 11
12Term 12
13Term 13
14Term 14
15Term 15
16Term 16
17Term 17
18Term 18
19Term 19
20Term 20
Sheet1
 
Upvote 0
Try with VBA
Alt-F11 to open VBA window, insert module then paste below code.
Hit F5 to run, or create a button on worksheet then assign macro to this code:

VBA Code:
Option Explicit
Sub RandomGroup()
Dim i&, r&, rng, s As String, s1 As String
rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
Randomize
r = Int(Rnd * UBound(rng)) + 1
s = rng(r, 1) & ", "
For i = 2 To 13
    Do
        r = Int(Rnd * UBound(rng)) + 1
        s1 = rng(r, 1) & ", "
    Loop Until InStr(1, ", " & s, ", " & s1) = 0
    s = s & s1
Next
Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = Left(s, Len(s) - 2)
Columns(3).AutoFit
End Sub

RandomGroup.xlsm
ABC
1Term 1
2Term 2Term 2, Term 19, Term 10, Term 18, Term 11, Term 5, Term 6, Term 13, Term 4, Term 9, Term 16, Term 14, Term 12
3Term 3
4Term 4
5Term 5
6Term 6
7Term 7
8Term 8
9Term 9
10Term 10
11Term 11
12Term 12
13Term 13
14Term 14
15Term 15
16Term 16
17Term 17
18Term 18
19Term 19
20Term 20
Sheet1

Hey thanks!
That seems to run perfectly for my needs. However, I need to carry the randomization throughout a long list of products. I am not very familiar with VBA but I am learning. How would I carry this macro down a list? Example:

1663912357070.png


Typically, I would click the little black box in the corner and drag it down but that just repeats the same cell data in this case.

Thanks again for the help!
 
Upvote 0
Try again:
It dupplicate to,i.e, 100 lines
(in code: num=100 , then adjust to what ever)
VBA Code:
Option Explicit
Sub RandomGroup()
Dim i&, j&, num&, r&, rng, s As String, s1 As String
rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
num = 100 ' number of results. change it to what ever you need.
Randomize
Do
    j = j + 1
    r = Int(Rnd * UBound(rng)) + 1
    s = rng(r, 1) & ", "
    For i = 2 To 13
        Do
            r = Int(Rnd * UBound(rng)) + 1
            s1 = rng(r, 1) & ", "
        Loop Until InStr(1, ", " & s, ", " & s1) = 0
        s = s & s1
    Next
    Cells(j + 1, "F").Value = Left(s, Len(s) - 2)
Loop Until j = num
Columns(6).AutoFit
End Sub
 
Upvote 0
Solution
Try again:
It dupplicate to,i.e, 100 lines
(in code: num=100 , then adjust to what ever)
VBA Code:
Option Explicit
Sub RandomGroup()
Dim i&, j&, num&, r&, rng, s As String, s1 As String
rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
num = 100 ' number of results. change it to what ever you need.
Randomize
Do
    j = j + 1
    r = Int(Rnd * UBound(rng)) + 1
    s = rng(r, 1) & ", "
    For i = 2 To 13
        Do
            r = Int(Rnd * UBound(rng)) + 1
            s1 = rng(r, 1) & ", "
        Loop Until InStr(1, ", " & s, ", " & s1) = 0
        s = s & s1
    Next
    Cells(j + 1, "F").Value = Left(s, Len(s) - 2)
Loop Until j = num
Columns(6).AutoFit
End Sub
Thats awesome thank you!!
 
Upvote 0
Try again:
It dupplicate to,i.e, 100 lines
(in code: num=100 , then adjust to what ever)
VBA Code:
Option Explicit
Sub RandomGroup()
Dim i&, j&, num&, r&, rng, s As String, s1 As String
rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
num = 100 ' number of results. change it to what ever you need.
Randomize
Do
    j = j + 1
    r = Int(Rnd * UBound(rng)) + 1
    s = rng(r, 1) & ", "
    For i = 2 To 13
        Do
            r = Int(Rnd * UBound(rng)) + 1
            s1 = rng(r, 1) & ", "
        Loop Until InStr(1, ", " & s, ", " & s1) = 0
        s = s & s1
    Next
    Cells(j + 1, "F").Value = Left(s, Len(s) - 2)
Loop Until j = num
Columns(6).AutoFit
End Sub
Actually that doesn't work. It just hangs my program. Tried it with a small number of 2. Still just hangs it

Edit: NVM the issue was there wasn't enough data in 'column A'. It works perfectly. Thank you!
 
Last edited:
Upvote 0
What is "num" value? still 100 or 1 milion?
Could you post current code and actual data in column A?
 
Upvote 0
What is "num" value? still 100 or 1 milion?
Could you post current code and actual data in column A?
I made an edit to my comment you must not have seen. The issue was there wasnt enough data in column A and it broke the script. After I added more data to A it worked well :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top