Macro to generate nmbers

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have numbers 1 to 52. I want to generate 6 randon numbers per row in Cols A to F from 1 to 32 on a random basis

My Code generates 10 numbers per row for Col A to F but cannot get it to work correctly

Kindly amend my code

Code:
 Sub GenerateLottoNumbers()
    Dim rng As Range
    Dim i As Integer, j As Integer
    
    ' Set the range where you want to generate the Lotto numbers
    Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:F6") ' Adjust the sheet name and range as needed
    
    ' Clear previous data in the range
    rng.ClearContents
    
    ' Loop through each row in the range
    For i = 1 To rng.Rows.Count
        ' Loop through each column in the row
        For j = 1 To 6
            ' Generate 6 random numbers between 1 and 32
            rng.Cells(i, j).Value = Int((32 - 1 + 1) * Rnd + 1)
        Next j
    Next i
End Sub
 

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.
Hi Howard,
Replace 32 to 52 in this row:

rng.Cells(i, j).Value = Int((32 - 1 + 1) * Rnd + 1) change to rng.Cells(i, j).Value = Int((52 - 1 + 1) * Rnd + 1)

But i recommend to change the code to choose only unique numbers...
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,129
Members
449,097
Latest member
mlckr

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