Macro to generate nmbers

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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