Extracting number from a range randomly

Goalexcel

Board Regular
Joined
Dec 28, 2020
Messages
101
Office Version
  1. 2016
Platform
  1. Windows
Hello Expert, please kindly advise what is the best method to extract numbers from a range randomly
I have a series of numbers in the range of A1 to T16. I would like to extract 3 differents numbers from each columns, and place them in column A21, B21so on. I wonder. if there is a way to do that easily.
 
when we pick up numbers by second time, we no need to repeat the numbers.
I don't know what you mean. Neither of the suggested codes allow to pick the same number more than once. Perhaps I am not understanding exactly what you are trying to do. You would need to spell it out in more detail..

Remember that your question was:
I would like to extract 3 differents numbers from each columns, and place them in column A21, B21so on.
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hello Expert, sorry for misunderstandy. I am new VBA, please help to find the solution, I forget to explained that I need to randomly choose numbers until all numbers I had called, so first choose 3 numbers, next time 3 or 4 differents numbers,

Here is the file below with one code.

1. Two codes I already used and codes are repeating the numbers. We do not need duplicates the numbers.
2. Workbook has 5 worksheets, every worksheet has different ranges.

Thank you for your understanding and help.
 
Last edited:
Upvote 0
next time 3 or 4 differents numbers,
How would we decide if it will be 3 or 4 each time?

I forget to explained that I need to randomly choose numbers until all numbers I had called
Where will each set of results be displayed?
Are the colours cleared from the top section before the next group of 3 (or 4) is chosen?
 
Upvote 0
Try this updated code:

VBA Code:
Sub pickRand()
    Dim i As Long, j As Long, r As Long, s As Boolean, c As Long, v, d, a, colors, col As Long
    Dim num
    num = 3 'default number of random cells to pick
    num = InputBox("How many random cells to pick?", , num)
    If Not IsNumeric(num) Then Exit Sub
    colors = Array(0, vbYellow, vbGreen, vbBlue, vbCyan, vbMagenta)
    
    Set d = CreateObject("scripting.dictionary")
    Randomize
    Application.ScreenUpdating = False
    With ActiveSheet
        .Range("A20:T20").Value = .Range("A1:T1").Value
        For i = 1 To 20 '20 columns = A:T, expand as you wish
            ReDim a(1 To 1)
            c = 1
            col = 0
            For j = 2 To 17
                If .Cells(j, i).Interior.ColorIndex = xlNone Then
                    ReDim Preserve a(1 To c)
                    a(c) = j
                    c = c + 1
                Else
                    For r = LBound(colors) To UBound(colors)
                        If colors(r) = .Cells(j, i).Interior.Color Then
                            If col < r Then col = r
                        End If
                    Next
                End If
            Next
            If UBound(a) < num Then
                MsgBox "Not enough options left!"
                Exit For
            End If
            d.RemoveAll
            s = False
            c = 0
            If col <= 4 Then
                col = colors(col + 1)
            Else
                col = vbRed
            End If
            Do
                r = CLng(((Rnd * 1000000) Mod UBound(a))) + 1
                If Not d.exists(a(r)) Then
                    d(a(r)) = 1
                    s = True
                    c = c + 1
                End If
                DoEvents
            Loop While s = False Or (s = True And c < num)
            c = 0
            r = .Cells(.Rows.Count, i).End(xlUp).Row + 1
            For Each v In d.keys
                .Cells(r + c, i).Value = .Cells(CInt(v), i).Value
                .Cells(r + c, i).Interior.Color = col
                .Cells(CInt(v), i).Interior.Color = col
                c = c + 1
            Next
        Next
    End With
    Application.ScreenUpdating = True
End Sub
It now asks how many random cells to pick, default is 3.
It now colours the randomly selected cells with up to 6 colours. You can change the colours to those you like here: vbYellow, vbGreen, vbBlue, vbCyan, vbMagenta, vbRed. See: RGB function (Visual Basic for Applications)
You can run the macro several times, depending on how many options remaining, and number of cells to pick. Each time it's run a different colour will be used to highlight the cells, and all output will be shown below with its respectively colour as well.
If the number of options remaining is less than the number of cells to pick, a message will be shown.
 
Upvote 0
How would we decide if it will be 3 or 4 each time?


Where will each set of results be displayed?
Are the colours cleared from the top section before the next group of 3 (or 4) is chosen?
1. Random numbers to choose, first time 3 , second time 3 or 4 numbers, depend for management decision.
2. The top section always has be colours, to make sure we already pick that numbers.
3. After I pick the numbers, I have another workbooks reports that will contained that numbers.
Thank you.
 
Upvote 0
Try this updated code:

VBA Code:
Sub pickRand()
    Dim i As Long, j As Long, r As Long, s As Boolean, c As Long, v, d, a, colors, col As Long
    Dim num
    num = 3 'default number of random cells to pick
    num = InputBox("How many random cells to pick?", , num)
    If Not IsNumeric(num) Then Exit Sub
    colors = Array(0, vbYellow, vbGreen, vbBlue, vbCyan, vbMagenta)
   
    Set d = CreateObject("scripting.dictionary")
    Randomize
    Application.ScreenUpdating = False
    With ActiveSheet
        .Range("A20:T20").Value = .Range("A1:T1").Value
        For i = 1 To 20 '20 columns = A:T, expand as you wish
            ReDim a(1 To 1)
            c = 1
            col = 0
            For j = 2 To 17
                If .Cells(j, i).Interior.ColorIndex = xlNone Then
                    ReDim Preserve a(1 To c)
                    a(c) = j
                    c = c + 1
                Else
                    For r = LBound(colors) To UBound(colors)
                        If colors(r) = .Cells(j, i).Interior.Color Then
                            If col < r Then col = r
                        End If
                    Next
                End If
            Next
            If UBound(a) < num Then
                MsgBox "Not enough options left!"
                Exit For
            End If
            d.RemoveAll
            s = False
            c = 0
            If col <= 4 Then
                col = colors(col + 1)
            Else
                col = vbRed
            End If
            Do
                r = CLng(((Rnd * 1000000) Mod UBound(a))) + 1
                If Not d.exists(a(r)) Then
                    d(a(r)) = 1
                    s = True
                    c = c + 1
                End If
                DoEvents
            Loop While s = False Or (s = True And c < num)
            c = 0
            r = .Cells(.Rows.Count, i).End(xlUp).Row + 1
            For Each v In d.keys
                .Cells(r + c, i).Value = .Cells(CInt(v), i).Value
                .Cells(r + c, i).Interior.Color = col
                .Cells(CInt(v), i).Interior.Color = col
                c = c + 1
            Next
        Next
    End With
    Application.ScreenUpdating = True
End Sub
It now asks how many random cells to pick, default is 3.
It now colours the randomly selected cells with up to 6 colours. You can change the colours to those you like here: vbYellow, vbGreen, vbBlue, vbCyan, vbMagenta, vbRed. See: RGB function (Visual Basic for Applications)
You can run the macro several times, depending on how many options remaining, and number of cells to pick. Each time it's run a different colour will be used to highlight the cells, and all output will be shown below with its respectively colour as well.
If the number of options remaining is less than the number of cells to pick, a message will be shown.
Good day Sir, Thank you for your fast replay,
can you please check the code no show numbers and colours en A21, B21 so on,
the code before had for cell A21: Cells(21 + c, i).Value = .Cells(v(c), i).Value '21 = starting row to output selected cells
 
Upvote 0

Attachments

  • Row21.PNG
    Row21.PNG
    53.4 KB · Views: 9
Upvote 0
Did you amend the code somehow? On my side it looks fine.

Book8
ABCDEFGHIJKLMNOPQRST
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20
2-0.20456-0.19373-0.25003-0.49253-0.325470.1356850.356998-0.00264-0.08576-0.55376-0.180010.39639-0.34186-0.779270.4531440.053439-0.608230.241259-0.15357-0.40707
30.4198370.413963-0.387440.3519150.340654-0.01442-0.335310.5166160.4758580.5174320.20221-0.10014-0.685410.274551-0.512570.728542-0.143440.007939-0.026190.571704
4-0.192340.649039-0.161690.5265010.1457610.3320510.244595-0.67988-0.082990.5253260.804667-0.473940.133580.15393-0.273990.776069-0.35417-0.848180.567303-0.32123
5-0.65863-0.42948-0.177640.0454140.1531630.562706-0.049090.351745-0.051160.5199730.5864670.5836680.4379460.412259-0.952510.1539010.5338130.444507-0.368320.113606
60.658359-0.477440.079638-0.68509-0.164050.2485530.4629260.264320.575878-0.18993-0.52572-0.02512-0.21529-0.43403-0.097160.655599-0.131890.433494-0.266090.028007
7-0.099470.2180130.3561720.658339-0.11289-0.60229-0.11530.407994-0.70747-0.07604-0.25771-0.49771-0.218370.6043390.142514-0.398720.0539040.3557670.0264680.204036
8-0.847020.1082410.213003-0.37339-0.51994-0.065780.52514-0.583610.797329-0.67074-0.18486-0.14702-0.50627-0.069950.510123-0.59941-0.01250.0072220.397795-0.25069
90.1662660.0838080.093319-0.43526-0.097770.8052170.3433170.2833270.37102-0.728990.7317630.58073-0.176790.3203220.191063-0.223520.655285-0.16406-0.30026-0.07658
100.114534-0.145130.011517-0.55592-0.96727-0.607480.402459-0.01554-0.49645-0.406980.44137-0.698180.477082-0.609840.3470640.1486940.159058-0.29949-0.16937-0.7676
11-0.55109-0.20373-0.325-0.047910.013116-0.04044-0.03443-0.468380.017259-0.36931-0.2129-0.20724-0.14671-0.504630.277776-0.25775-0.207370.7445240.125087-0.12812
120.4648160.192227-0.707140.083567-0.35439-0.7224-0.670060.50135-0.63283-0.94458-0.080320.0855910.4387130.1402550.008622-0.27475-0.21443-0.20076-0.273270.259962
130.0890280.8946330.622917-0.006170.610865-0.44815-0.36821-0.64193-0.05979-0.12454-0.52973-0.54735-0.2884-0.26205-0.74437-0.070520.320825-0.159950.347749-0.02713
140.793659-0.282010.236423-0.48210.9702240.7085420.11317-0.44130.363422-0.083660.2905560.06723-0.535090.3193130.259279-0.15310.029304-0.10073-0.621950.803779
150.0741640.206724-0.074990.679309-0.50035-0.73551-0.410970.491063-0.08834-0.084730.1305220.2880560.541843-0.04726-0.11835-0.145930.9320490.424861-0.46935-0.15175
16-0.240250.154940.2394540.3915790.76713-0.16127-0.050630.211509-0.696580.159646-0.13365-0.08576-0.21426-0.717320.557173-0.239230.7780110.0221660.334188-0.78815
170.093311-0.84262-0.65747-0.24796-0.11760.362947-0.07881-0.67221-0.21275-0.12639-0.729530.081411-0.762510.174395-0.02346-0.028980.4792360.471919-0.62784-0.12057
18
19
20Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20
210.419837-0.20373-0.38744-0.00617-0.500350.3629470.402459-0.672210.37102-0.94458-0.180010.58073-0.218370.319313-0.09716-0.239230.533813-0.84818-0.153570.204036
22-0.847020.154940.622917-0.49253-0.32547-0.735510.244595-0.4413-0.49645-0.18993-0.52572-0.20724-0.2884-0.04726-0.023460.7285420.4792360.241259-0.30026-0.02713
230.793659-0.47744-0.16169-0.37339-0.09777-0.01442-0.33531-0.64193-0.212750.525326-0.729530.0814110.13358-0.06995-0.118350.655599-0.214430.007222-0.27327-0.40707
240.1145340.108241-0.17764-0.48210.0131160.7085420.11317-0.01554-0.70747-0.40698-0.18486-0.497710.541843-0.434030.557173-0.02898-0.60823-0.20076-0.02619-0.25069
25-0.55109-0.429480.0933190.5265010.76713-0.60229-0.07881-0.002640.017259-0.369310.20221-0.10014-0.762510.2745510.2592790.776069-0.354170.7445240.567303-0.7676
26-0.204560.0838080.3561720.0835670.145761-0.7224-0.03443-0.67988-0.632830.159646-0.080320.06723-0.214260.6043390.191063-0.274750.3208250.471919-0.169370.259962
Sheet14
 
Upvote 0
Did you amend the code somehow? On my side it looks fine.

Book8
ABCDEFGHIJKLMNOPQRST
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20
2-0.20456-0.19373-0.25003-0.49253-0.325470.1356850.356998-0.00264-0.08576-0.55376-0.180010.39639-0.34186-0.779270.4531440.053439-0.608230.241259-0.15357-0.40707
30.4198370.413963-0.387440.3519150.340654-0.01442-0.335310.5166160.4758580.5174320.20221-0.10014-0.685410.274551-0.512570.728542-0.143440.007939-0.026190.571704
4-0.192340.649039-0.161690.5265010.1457610.3320510.244595-0.67988-0.082990.5253260.804667-0.473940.133580.15393-0.273990.776069-0.35417-0.848180.567303-0.32123
5-0.65863-0.42948-0.177640.0454140.1531630.562706-0.049090.351745-0.051160.5199730.5864670.5836680.4379460.412259-0.952510.1539010.5338130.444507-0.368320.113606
60.658359-0.477440.079638-0.68509-0.164050.2485530.4629260.264320.575878-0.18993-0.52572-0.02512-0.21529-0.43403-0.097160.655599-0.131890.433494-0.266090.028007
7-0.099470.2180130.3561720.658339-0.11289-0.60229-0.11530.407994-0.70747-0.07604-0.25771-0.49771-0.218370.6043390.142514-0.398720.0539040.3557670.0264680.204036
8-0.847020.1082410.213003-0.37339-0.51994-0.065780.52514-0.583610.797329-0.67074-0.18486-0.14702-0.50627-0.069950.510123-0.59941-0.01250.0072220.397795-0.25069
90.1662660.0838080.093319-0.43526-0.097770.8052170.3433170.2833270.37102-0.728990.7317630.58073-0.176790.3203220.191063-0.223520.655285-0.16406-0.30026-0.07658
100.114534-0.145130.011517-0.55592-0.96727-0.607480.402459-0.01554-0.49645-0.406980.44137-0.698180.477082-0.609840.3470640.1486940.159058-0.29949-0.16937-0.7676
11-0.55109-0.20373-0.325-0.047910.013116-0.04044-0.03443-0.468380.017259-0.36931-0.2129-0.20724-0.14671-0.504630.277776-0.25775-0.207370.7445240.125087-0.12812
120.4648160.192227-0.707140.083567-0.35439-0.7224-0.670060.50135-0.63283-0.94458-0.080320.0855910.4387130.1402550.008622-0.27475-0.21443-0.20076-0.273270.259962
130.0890280.8946330.622917-0.006170.610865-0.44815-0.36821-0.64193-0.05979-0.12454-0.52973-0.54735-0.2884-0.26205-0.74437-0.070520.320825-0.159950.347749-0.02713
140.793659-0.282010.236423-0.48210.9702240.7085420.11317-0.44130.363422-0.083660.2905560.06723-0.535090.3193130.259279-0.15310.029304-0.10073-0.621950.803779
150.0741640.206724-0.074990.679309-0.50035-0.73551-0.410970.491063-0.08834-0.084730.1305220.2880560.541843-0.04726-0.11835-0.145930.9320490.424861-0.46935-0.15175
16-0.240250.154940.2394540.3915790.76713-0.16127-0.050630.211509-0.696580.159646-0.13365-0.08576-0.21426-0.717320.557173-0.239230.7780110.0221660.334188-0.78815
170.093311-0.84262-0.65747-0.24796-0.11760.362947-0.07881-0.67221-0.21275-0.12639-0.729530.081411-0.762510.174395-0.02346-0.028980.4792360.471919-0.62784-0.12057
18
19
20Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20
210.419837-0.20373-0.38744-0.00617-0.500350.3629470.402459-0.672210.37102-0.94458-0.180010.58073-0.218370.319313-0.09716-0.239230.533813-0.84818-0.153570.204036
22-0.847020.154940.622917-0.49253-0.32547-0.735510.244595-0.4413-0.49645-0.18993-0.52572-0.20724-0.2884-0.04726-0.023460.7285420.4792360.241259-0.30026-0.02713
230.793659-0.47744-0.16169-0.37339-0.09777-0.01442-0.33531-0.64193-0.212750.525326-0.729530.0814110.13358-0.06995-0.118350.655599-0.214430.007222-0.27327-0.40707
240.1145340.108241-0.17764-0.48210.0131160.7085420.11317-0.01554-0.70747-0.40698-0.18486-0.497710.541843-0.434030.557173-0.02898-0.60823-0.20076-0.02619-0.25069
25-0.55109-0.429480.0933190.5265010.76713-0.60229-0.07881-0.002640.017259-0.369310.20221-0.10014-0.762510.2745510.2592790.776069-0.354170.7445240.567303-0.7676
26-0.204560.0838080.3561720.0835670.145761-0.7224-0.03443-0.67988-0.632830.159646-0.080320.06723-0.214260.6043390.191063-0.274750.3208250.471919-0.169370.259962
Sheet14
Hi, I dont modify the code, copy and paste from this forum. can you please insert the code again, because I have the same result, missing lower number A21.
 

Attachments

  • Row21.PNG
    Row21.PNG
    53.4 KB · Views: 9
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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