Random generator giving script out of range

sjtjr73

New Member
Joined
May 1, 2020
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
I am having trouble trying to coping the random generated data that I collected. I get Subscript out of range. I know what it from. But I can't seem to over come it. I have a userform with a textbox and 2 button. The first button creates a new worksheet called FW19 or what is entered in the textbox. The other button runs the random data gather and copy it to a sheet. But I am having trouble calling the new sheet and I get the script out of range. Code below.
VBA Code:
Option Explicit
Private Sub cmdBut1_Click()
Dim strFW As String
Dim LR As String
Dim ws As Worksheet
'' Creats Sheet name
strFW = UserForm1.txtbox1.Value

If strFW <> "" Then
  Set ws = ThisWorkbook.Sheets.Add
  End If
 '' Creats txt in cells Al and B1
 If strFW <> " " Then
 ws.Name = strFW
Range("$B$1").Value = txtbox1.Value
Range("$A$1").Value = "Fiscal Week"
End If
 

End Sub
''Random Generator
Private Sub CmdBut2_Click()
Dim LastRow As Long
Dim NbRows As Long
Dim RowList()
Dim I As Long, J As Long, K As Long
Dim RowNb As Long
   
    Sheets("INV_Data").Activate
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    NbRows = IIf(LastRow < 8000, LastRow * 0.003, 30)
    ReDim RowList(1 To NbRows)
    K = 1
    For I = 1 To NbRows
        RowNb = Rnd() * LastRow
        For J = 1 To K
            If (RowList(J) = RowNb) Then GoTo NextStep
        Next J
        RowList(K) = RowNb
        '' below is the line I am having trouble with. When it gives me the erroe I hover
        ''over("strFW") and it shows me what ws it wants to copy to
        Rows(RowNb).Copy Destination:=Sheets("strFW")(K + 2, "C")
        K = K + 1
NextStep:
    Next I
End Sub
 
You're welcome & thanks for the feedback.
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,214,424
Messages
6,119,407
Members
448,894
Latest member
spenstar

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