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
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi & welcome to MrExcel.
Remove this line
VBA Code:
Dim strFW As String
from the cmdBut1 code & put it just below Option Explicit. Then in the CmdBut2 code use
VBA Code:
Rows(RowNb).Copy Destination:=Sheets(strFW).Cells(k + 2, "A")
 
Upvote 0
Still gets the same error. IT acts like it not seeing the new work sheet
 
Upvote 0
What is your code now?
 
Upvote 0
VBA Code:
Option Explicit
Dim strFW As String


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).Cells(K + 2, "A")
        K = K + 1
NextStep:
    Next I
End Sub
 
Upvote 0
I forgot to take it out some were else. it works know. Thanks for the Help
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
One more question. I want to add name in Cell A2,B2. I know the code for and added it to the end before Unload Me is called. But the value did not get placed.
VBA Code:
Option Explicit
Dim strFW As String


Private Sub cmdBut1_Click()
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).Cells(K + 2, "A")
        K = K + 1
NextStep:
    Next I
 Range("A2").Value = "Record"
 Range("B2").Value = "Description"
 Range("C2").Value = "Location"
 
    
    Unload Me
End Sub

[CODE=vba]
should I add before close?
 
Upvote 0
Assuming you want those on the new sheet just add
VBA Code:
Sheets(strFW)
before the range, like
VBA Code:
Sheets(strFW).Range("A2").Value="Record"
 
Upvote 0
Fluff, Thanks for all the help. It working like a charm. Their only 2 things I am making the user do manually, because I could not figure it out. When the new FW sheet get made and data populates I making her opening the columns up they she can read everything and she might have to make sure the new sheets are in order.
 
Upvote 0

Forum statistics

Threads
1,215,212
Messages
6,123,649
Members
449,111
Latest member
ghennedy

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