Why is

JFUCOCHAN

New Member
Joined
Jun 12, 2003
Messages
33
I'm running the following code which is taken from

http://www.mrexcel.com/board2/viewtopic.php?p=1112482&highlight=rand#1112482


Dim MY_RND_NO(80) As Variant
Sub CREATE_RANDOM()
Randomize
Range("C1:C100").ClearContents
MY_COUNT = 1
Do Until MY_COUNT = 101
NEW_NUMBER = Int(Rnd() * (561 - 1) + 1)
If MY_RND_NO(NEW_NUMBER) <> "USED" Then
Range("C" & MY_COUNT).Value = NEW_NUMBER
MY_RND_NO(NEW_NUMBER) = "USED"
MY_COUNT = MY_COUNT + 1
End If
Loop
End Sub


I get a subscript out of range error because i'm trying to run this for 100 cells and over the range from 1-561. any thoughts?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

DauntingGecko

Well-known Member
Joined
Feb 2, 2006
Messages
521
Code:
If MY_RND_NO(NEW_NUMBER) <> "USED" Then

What is "USED" ??? Thats what it doesn't understand. It doesn't know what this is.
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hi,

the problem is you are asking for an item of the array which doesn't exist

the dimension of the array is "80"
when you ask to chech it item "542" has "used" in it, the bug is telling you that arrayitem 542 doesn't exist

in fact this is not so good code to find random numbers, since it will perfrom numerous unneeded loops which can be avoided

you can find other code on the board

kind regards,
Erik
 

patrickmuldoon99

Active Member
Joined
Jun 27, 2006
Messages
345
I'm really quite unsure what you are trying to do here, or even the intention of the original post:

As I understand it you want to generate unique random numbers? Is that correct?

That would explain the "USED" thing, as you are writing in the variant array "USED" so as to avoid generating a new random number if it has already been created.

To avoid your error, you should redefine that MY_RND_NO array at the top so that it accounts for 561 elements. e.g:

Code:
dim MY_RND_NO(561) as variant
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
I do not have much time at the moment so this is a quickie.
there is better code here on the Board, but I found it nice to show an alternative method :)

no loops involved
Code:
Option Explicit

Sub generate_rnd(LowNr As Long, DiffNr As Long, Cnt As Long)
'Erik Van Geit
'060929
'will be slow when large range involved

Dim WB As Workbook
Dim arr As Variant

    If Cnt > DiffNr Then
    MsgBox "Cnt > DiffNr", 48, "ERROR"
    Exit Sub
    End If
    
    If DiffNr > Rows.Count Then
    MsgBox "DiffNr > " & Rows.Count, 48, "ERROR"
    Exit Sub
    End If

Application.ScreenUpdating = False

Workbooks.Add
Set WB = ActiveWorkbook
    
    With Range("A" & LowNr + 1 & ":A" & LowNr + Cnt)
    .Cells(1, 1).FormulaArray = _
    "=SMALL(IF(COUNTIF(R" & LowNr & "C1:R[-1]C,ROW(R" & LowNr & ":R" & DiffNr + LowNr - 1 & "))<>1," & _
    "ROW(R" & LowNr & ":R" & DiffNr + LowNr - 1 & ")),1+INT(RAND()*(" & DiffNr & "-ROW()+ROW(R" & LowNr + 1 & "C1))))"
    .Cells(1, 1).Copy .Offset(1, 0).Resize(Cnt, 1)
    arr = .Value
    End With
    WB.Close False
    
ActiveSheet.Cells(1, 1).Resize(Cnt, 1) = arr

Application.ScreenUpdating = True

End Sub

Sub test()
Call generate_rnd(10, 500, 30)
End Sub
run the "testmacro"
first number is lowest value
second is the "range"
third is the number of values you want to generate

in this example you will generate 30 values between 10 and 510

best regards,
Erik
 

Forum statistics

Threads
1,136,989
Messages
5,678,975
Members
419,796
Latest member
doctorgresham

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
Top