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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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