Generate random numbers and highlight those numbers in spreadsheet.

Johncobb

New Member
Joined
Sep 7, 2006
Messages
43
Office Version
  1. 365
Platform
  1. Windows
In my spreadsheet I have many rows of 40 numbers in each row (numbers between 1 and 80 in each row).
I want to generate 20 random numbers between 1 and 80 and then to check if they come up in each row (each row has 40 numbers between 1 and 80).
The numbers that came up should be highlighted in each row in a color so I will be able to see which numbers and how many numbers did come up after each random spin.
I hope that you understand what I mean. This is a short description. Please contact me if you need more explanations.
Waiting for comments and advice.
John
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
What is the exact range that you would like these 20 random numbers to appear in?
Do you want to create all 20 at once, or one at a time?

Where are these rows of 40 numbers?
What row do they start on?
Do they start in column A and go out to column AN?
 
Upvote 0
OK, I came up with something that should work.
You may have to modify slightly to fit your structure.

Let's say that you are wanting to populate the range A1:T1 (20 cells) with unrepeating numbers from 1-80, one at a time.
And let's say that you have a bunch of rows of data of 40 numbers, starting at cell A10 and going down.

The first thing we want to do is to set up Conditional Formatting on our current rows of data, to highlight the numbers as they get selected.
So, starting with cell A10, select ALL the rows and columns of existing numbers that you want to check.
Then, go to Conditional Formatting, select New Rule, then "Use a formula to determine which cells to format", and enter this formula:
Excel Formula:
=COUNTIF($A$1:$T$1,A10)>0
then click the Format button, go to the Fill tab, select the color you want to highlight the cell in, and click OK.

Then, add this VBA code to the workbook:
VBA Code:
Sub PopulateRandomNum()
    
    Dim rng As Range
    Dim lcol As Long
    Dim rn As Long
    Dim cell As Range
    Dim ct As Long
    
'   Designate range to populate values in
    Set rng = Range("A1:T1")
    
'   Check to see if all columns filled in
    If Range("T1") <> "" Then
        MsgBox "All cells have been populated with random numbers", vbOKOnly, "STOP!"
        Exit Sub
    End If
    
'   Find next cell to populate
    If Range("A1") = "" Then
        Set cell = Range("A1")
    Else
        Set cell = Range("U1").End(xlToLeft).Offset(0, 1)
    End If

'   Populate next cell
    Do
'       Calculate random number
        rn = CreateRandomNum(1, 80)
'       Check to see if it is not found in range
        If Application.WorksheetFunction.CountIf(rng, rn) = 0 Then
'           Populate cell with random number
            cell.Value = rn
'           Exit loop
            Exit Do
        End If
'       Fail-safe to make sure you do not get caught in endless loop
        ct = ct + 1
        If ct = 200 Then
            MsgBox "Process failed and quit after 200 attempts.", vbOKOnly, "ERROR!"
            Exit Do
        End If
    Loop

End Sub


Function CreateRandomNum(lb As Long, ub As Long) As Long
    Randomize
    CreateRandomNum = Int((ub - lb + 1) * Rnd + lb)
End Function
If you like, you can add a button to your worksheet, and attach the "PopulateRandomNum" VBA procedure to the button, or assign a keyboard shortcut to this code to quickly and easily run it (by clicking the button or using the keyboard shortcut).

Each time you run it, it will populate the range A1:T1, one cell at a time.
And the Conditional Formatting will be invoked automatically and highlight the numbers, as they appear in your list.
 
Upvote 0
What is the exact range that you would like these 20 random numbers to appear in?
Do you want to create all 20 at once, or one at a time?

Where are these rows of 40 numbers?
What row do they start on?
Do they start in column A and go out to column AN?
Hi Joe4,
Thank you for your reply.
I don't know much about VBA code and where to put it. I will answer your above questions.

The total range is A1:AN291
Automatically selected (between 1 and 80) 20 random numbers at once. This 20 numbers will be checked if they came up in each row and each row has 40 numbers.
First row starts at A1
Columns run from A to AN

I have something similar created before but I can not modify it for this spreadsheet.
In the previous one I entered a formula in cells AO1 to AO20 and that's where the random numbers were created, then they were searched in each row and highlighted with a colored cell.
In next column AP there was another formula that has showed how many numbers did come up in each row.
From memory I think that all this was done with formulas. Unfortunately I am not good at it therefore I have to bug you mr. expert :)
Thank you in advance for your help and if you need more explanations please let me know.
John
 
Upvote 0
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
I have applied as per your instructions.
Only one number is generated and highlighted. There must be an error somewhere.
Below I provide copy what I have applied. Please check for errors. Thank you in advance.


Excel Formula:

=COUNTIF($AO$1:$AO$20,A1)>0

VBA Code:

Sub PopulateRandomNum()

Dim rng As Range
Dim lcol As Long
Dim rn As Long
Dim cell As Range
Dim ct As Long

' Designate range to populate values in
Set rng = Range("AO1:AO20")

' Check to see if all columns filled in
If Range("AO20") <> "" Then
MsgBox "All cells have been populated with random numbers", vbOKOnly, "STOP!"
Exit Sub
End If

' Find next cell to populate
If Range("AO1") = "" Then
Set cell = Range("AO1")
Else
Set cell = Range("AO20").End(xlToLeft).Offset(0, 1)
End If

' Populate next cell
Do
' Calculate random number
rn = CreateRandomNum(1, 80)
' Check to see if it is not found in range
If Application.WorksheetFunction.CountIf(rng, rn) = 0 Then
' Populate cell with random number
cell.Value = rn
' Exit loop
Exit Do
End If
' Fail-safe to make sure you do not get caught in endless loop
ct = ct + 1
If ct = 200 Then
MsgBox "Process failed and quit after 200 attempts.", vbOKOnly, "ERROR!"
Exit Do
End If
Loop

End Sub


Function CreateRandomNum(lb As Long, ub As Long) As Long
Randomize
CreateRandomNum = Int((ub - lb + 1) * Rnd + lb)
End Function
 
Upvote 0
Is this what you want.
+Fluff 1.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBH
1806756364142406255242454753372153952474375283341230582873435269163819575535431360536220802117705021382882568686221
271298064217234503739177036342113797642291441242352253213180306056347703365737750366336392817836258137306422754219
378182116954484444193939613127671564442787425747916246235504333503360515561277119275771722070624680523972595811335014
4168051783563742852489112139406822116435683461064252753361570916432794261840606871306566148806263391473293519203
564170671716157876640684569451337102353254823376870312177156641321879792464241045709685344457336321634211260376363
67372523955354224771621772631402465561254622122312226452244242715837675556313744746402213714664512146317624537194150
71120643275844263628545057047591111247596050753857732934802939636555693139115334504576095944461585851155822404119
86245793680726576211701636171943533296343222761353356743354725281520522951438065615763538252022545314101486126552055
960411949686869559197060647325971417746137543572394344951211116603571665943260297580135610721812765574931435411625
106149493079643552566375363269781350717680251062122524442651879674973553853846612179232524442705536491175532369557366
1125363937037764419756722529917331131291252376836202215447772171483893866725725636142527638360325053152226733484773
121674435607862714066025048251943537144746452951207273723211387645624233611141897633552758662470763816125457437
134673643839216364285594366805631642868617764153782628626172737454630287375305472221813611551074217561542213319
144857762715375458703961387843726367231594646674476921270594917662926164610613414193875574944213683433491435701920
1577345471166631212572937191877837217750555957677274506033928727131612228672233775755848674744667664611261635702928
163301346346564506746571627332477137542146272567334275463577437442419723183638734664314251067172131880572162325256
1744655637104322622670614676343242451258070528375078174261870303862283266446015647959323924433642163264184933672447615
1853386032849742394636836821663669122266116630352417362206183015583733225161302880311363787177541638277847566730495
19623107356591436261775216633575277955234476416262157865304052604241531846148107645381547357057251443062956786562
20217462294311956517146855687158471437101923520231179301946334463405474248666085977076733323541251137228126682035
Main
Cell Formulas
RangeFormula
AO1:BH20AO1=RANDARRAY(20,20,1,80,1)
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:AN20Expression=COUNTIFS(A1,$AO1:$BH1)textNO
 
Upvote 0
Only one number is generated and highlighted. There must be an error somewhere.
No, none at all.
From your original wording, it sounded like you might want to do one at a time. So that is the way I created it, and I mentioned that:
Each time you run it, it will populate the range A1:T1, one cell at a time.

If you want to do all 20 at once, you can use this variation of the main procedure:
VBA Code:
Sub PopulateRandomNum()
   
    Dim rng As Range
    Dim rn As Long
    Dim cell As Range
    Dim ct As Long
   
'   Designate range to populate values in
    Set rng = Range("A1:T1")
   
'   Loop through each cell in range
    For Each cell In rng
        ct = 0
'       Populate next cell
        Do
'           Calculate random number
            rn = CreateRandomNum(1, 80)
'           Check to see if it is not found in range
            If Application.WorksheetFunction.CountIf(rng, rn) = 0 Then
'               Populate cell with random number
                cell.Value = rn
'               Exit loop
                Exit Do
            End If
'           Fail-safe to make sure you do not get caught in endless loop
            ct = ct + 1
            If ct = 200 Then
                MsgBox "Process failed and quit after 200 attempts.", vbOKOnly, "ERROR!"
                Exit Do
            End If
        Loop
    Next cell

End Sub
(Note that you still need the Function from my original code too).
 
Upvote 0
In my spreadsheet I have many rows of 40 numbers in each row (numbers between 1 and 80 in each row).
I want to generate 20 random numbers between 1 and 80 and then to check if they come up in each row (each row has 40 numbers between 1 and 80).
The numbers that came up should be highlighted in each row in a color so I will be able to see which numbers and how many numbers did come up after each random spin.
I hope that you understand what I mean. This is a short description. Please contact me if you need more explanations.
Waiting for comments and advice.
John
The 20 random numbers that you want to generate... will the same 20 numbers be highlighted throughout your table of numbers or do you want 20 random numbers generated and highlighted for each row individually?
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,260
Members
449,149
Latest member
mwdbActuary

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