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
 
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
No.
I have rows with numbers, 40 numbers in each row - range A1:AN291
I want to generate one set of 20 random numbers. - In my old project it was done in range AO1:AO20 with a formula =INT(RAND()*(80-1)+1).
I want a function or script to search each row and show me how many numbers did come up from those 20 randomly generated. Those numbers that match must be highlighted.
I hope that I explained correctly this time.
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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?
the same 20 numbers be highlighted throughout the table of numbers
Thank you. :)
 
Upvote 0
Ok, how about
+Fluff 1.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAO
180675636414240625524245475337215395247437528334123058287343526916381957553559
2712980642172345037391770363421137976422914412423522532131803060563477033657354
3781821169544844441939396131276715644427874257479162462355043335033605155612738
416805178356374285248911213940682211643568346106425275336157091643279426184017
5641706717161578766406845694513371023532548233768703121771566413218797924642410
673725239553542247716217726314024655612546221223122264522442427158376755563137425
711206432758442636285450570475911112475960507538577329348029396365556931391116
86245793680726576211701636171943533296343222761353356743354725281520522951438071
960411949686869559197060647325971417746137543572394344951211116603571665943277
1061494930796435525663753632697813507176802510621225244426518796749735538538466178
1125363937037764419756722529917331131291252376836202215447772171483893866725732
1216744356078627140660250482519435371447464529512072737232113876456242336158
13467364383921636428559436680563164286861776415378262862617273745463028736
144857762715375458703961387843726367231594646674476921270594917662926164610613452
1577345471166631212572937191877837217750555957677274506033928727131612228672223
163301346346564506746571627332477137542146272567334275463577437442419723183623
174465563710432262267061467634324245125807052837507817426187030386228326644601574
185338603284974239463683682166366912226611663035241736220618301558373322516115
196231073565914362617752166335752779552344764162621578653040526042415318461469
20217462294311956517146855687158471437101923520231179301946334463405474248666067
Main
Cell Formulas
RangeFormula
AO1:AO20AO1=RANDARRAY(20,,1,80,1)
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:AN20Expression=COUNTIFS($AO$1:$AO$20,A1)textNO
 
Upvote 0
Under you current conditions, the VBA code adjustment to my original code would look like:
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("AO1:AO20")
   
'   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


Function CreateRandomNum(lb As Long, ub As Long) As Long
    Randomize
    CreateRandomNum = Int((ub - lb + 1) * Rnd + lb)
End Function

And the Conditional Formatting rule would be the same one that Fluff posted.

So you should now have both VBA and non-VBA solutions to your problem.
 
Upvote 0
Solution
Here is another macro that you can try (uses only a single loop)...
VBA Code:
Sub Pick20RandNumsAndHighlightThem()
  Dim Cnt As Long, RandomIndex As Long, RndNums As Variant
  RndNums = [ROW(1:80)]
  Range("AO1").Resize(, 20).Clear
  Cells.Interior.ColorIndex = 0
  Application.ReplaceFormat.Clear
  Application.ReplaceFormat.Interior.Color = vbYellow
  For Cnt = 1 To 20
    RandomIndex = Application.RandBetween(Cnt, 80)
    Cells.Replace RndNums(RandomIndex, 1), "", xlWhole, , , , False, True
    Range("AN1").Offset(, Cnt) = RndNums(RandomIndex, 1)
    RndNums(RandomIndex, 1) = RndNums(Cnt, 1)
  Next
  Application.ReplaceFormat.Clear
End Sub
 
Upvote 0
Thank you experts for your help. Both codes work. :)
Now I would like to add one more thing.
At the end of the whole range of my numbers in column AP, at the end of each row of numbers, I would like that there would be displayed a count of numbers matched / highlighted on that row.
So I will not have to count matched and highlighted numbers in each row, but by looking at the column AP it will show me how many matched in each row.
And if the count number in column AP is 3 or 2 or 1 or 0 that number should be in red font.
I hope that you know what I mean.
 
Upvote 0
Place this formula in cell AP1 and copy down for all rows of data:
Excel Formula:
=SUM(COUNTIFS(A1:AN1,$AO$1:$AO$20))

Then, select the data in column AP from AP1 down to your last row of data (291?), and use this Conditional Formatting formula:
Excel Formula:
=AP1<=3
and choose the red font color for the Formatting.
 
Upvote 0
Place this formula in cell AP1 and copy down for all rows of data:
Excel Formula:
=SUM(COUNTIFS(A1:AN1,$AO$1:$AO$20))

Then, select the data in column AP from AP1 down to your last row of data (291?), and use this Conditional Formatting formula:
Excel Formula:
=AP1<=3
and choose the red font color for the Formatting.
Thank you very much again. It works.
One more thing (I hope this is the last one).
To modify my last request:
I want the same function but this time if the count number in column AP is 0 or 1 or 2 or 3 or 4 or 5 or 15 or 16 or 17 or 18 or 19 or 20 then the font should be red.
Thank you in advance.
 
Upvote 0
I want the same function but this time if the count number in column AP is 0 or 1 or 2 or 3 or 4 or 5 or 15 or 16 or 17 or 18 or 19 or 20 then the font should be red.
The Conditional Formatting rule should look something like:
Excel Formula:
=OR(AND(AP1>=0,AP1<=5),AND(AP1>=15,AP1<=20))
 
Upvote 0
I am glad that the last answer seemed to work for you.

However, in marking the "Solution" to the question, please mark the post that answered your original question, not the follow-ups.
Could you go back and mark the solution that you used for the original question you asked?

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,986
Members
449,058
Latest member
oculus

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