Macro to generate nmbers

howard

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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Your code seems to be OK.
For j = 1 To 6
This means it goes from 1st to 6th column. How come you have 10 numbers from A to F?
 
Upvote 0
Hi Howard, if I understand your need correctly, please try the following:
VBA Code:
Sub howard()
    Range("A1").CurrentRegion.ClearContents
    With Range("A1")
        .Formula2 = "=SORT(TRANSPOSE(INDEX(UNIQUE(RANDARRAY(6^2, 1, 1, 32, TRUE)), SEQUENCE(6))),1,1,1)"
        .Resize(10).FillDown
        With .CurrentRegion
            .Value = .Value
        End With
    End With
End Sub
 
Upvote 0
I should only have 6 numbers per column and Not 10 . Not Sure why it is generating 10 numbers
 
Upvote 0
Hi Howard,
I have just tweaked Kevin's code to meet your requirement as per my understanding of your post
VBA Code:
Sub howard()
    Range("A1").CurrentRegion.ClearContents
    With Range("A1")
        .Formula2 = "=TRANSPOSE(INDEX(UNIQUE(RANDARRAY(6^2, 1, 1, 52, TRUE)), SEQUENCE(6)))"
        .Resize(32).FillDown
        With .CurrentRegion
            .Value = .Value
        End With
    End With
End Sub
 
Upvote 0
1700804294853.png

Your first code works perfectly fine for me. Modify Kevins code for 6 rows per column:
VBA Code:
.Resize(6).FillDown
 
Upvote 0
Maybe this?
VBA Code:
Sub howard()
    Range("A1").CurrentRegion.ClearContents
    With Range("A1:F1")
        .Formula2 = "=INDEX(UNIQUE(RANDARRAY(6^2, 1, 1, 32, TRUE)), SEQUENCE(6))"
        With .CurrentRegion
            .Value = .Value
        End With
    End With
End Sub
Book1
ABCDEFG
122202342623
29292273027
3282128281921
44222532177
572315191828
626108222726
7
Sheet1
 
Upvote 0
Solution
Hi Howard,
I have just tweaked Kevin's code to meet your requirement as per my understanding of your post
VBA Code:
Sub howard()
    Range("A1").CurrentRegion.ClearContents
    With Range("A1")
        .Formula2 = "=TRANSPOSE(INDEX(UNIQUE(RANDARRAY(6^2, 1, 1, 52, TRUE)), SEQUENCE(6)))"
        .Resize(32).FillDown
        With .CurrentRegion
            .Value = .Value
        End With
    End With
End Sub
Thanks Sanjeev. I amended Code below

Code:
    .Resize(32).FillDown to 
[QUOTE]
    .Resize(6).FillDown
[/QUOTE]
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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