CT19X
CT19X

Reputation: 25

Runtime Error 9 - Subscript Out Of Range

I'm trying to create a form based interface using the VBA. I ran into a problem which I cannot figure it out. So in my code, I try to create a new workbook then save it. After that it will scan through the checkbox and see which one is getting selected. If it get selected, it will create a new sheet and copy the pre-made template. The user will open another workbook that they want and it will copy the information from that workbook to the workbook that just made. Here is the code for retrieving the data:

Sub mil10_data()

Dim NewWB As Workbook
Dim thisWB As Workbook
Dim wb As Workbook
Dim Ret

Set thisWB = ThisWorkbook
Set NewWB = ActiveWorkbook
With NewWB
    'Copy the pre-made template to new workbook
    thisWB.Sheets("Data 10").Range("A1:AZ3000").Copy NewWB.Sheets(ActiveSheet.Name).Range("A1:AZ3000")

    'Retriving the data
    Ret = Application.GetOpenFilename("Excel Files (*.CSV), *.CSV", Title:="Select File To Be Opened")
    If Ret = False Then Exit Sub
    Set wb = Workbooks.Open(Ret)
    **'This is where the error is show up
    wb.Sheets(ActiveSheet.Name).Range("E21:E2136").Copy NewWB.Sheets(ActiveSheet.Name).Range("C2:C2117")** 
    wb.Close SaveChanges:=False

    Set wb = Nothing
    Set NewWB = Nothing
    End With
End Sub

I figure that maybe because there are three workbooks open, it didn't know which one is the active workbook, but that is not the case. I tested by using the MsgBox and it display the right workbook and worksheeet name that I want. If I change the ActiveSheet.Name to the actual sheet name, it works, but I don't want to use that method. I have different worksheets that need to be created, so I prefer using the ActiveSheet.Name. Anyone know why it didn't work? I would really appreciate the help. Thank you!

Upvotes: 0

Views: 2029

Answers (1)

user2271770
user2271770

Reputation:

ActiveWorkbook and ActiveSheet are really clumsy ways of getting user input. You should save in variables the properties that are you interested in (as soon as possible), and then stop referring them directly.

In your case, the code may look like

Sub mil10_data()

    Dim NewWB As Workbook
    Dim thisWB As Workbook
    Dim wb As Workbook
    Dim Ret

    Dim active_sheet_name  As String

    Set thisWB = ThisWorkbook
    Set NewWB = ActiveWorkbook
    Let active_sheet_name = Application.ActiveSheet.Name

    With NewWB
        'Copy the pre-made template to new workbook
        thisWB.Sheets("Data 10").Range("A1:AZ3000").Copy NewWB.Sheets(active_sheet_name).Range("A1:AZ3000")

        'Retrieving the data
        Ret = Application.GetOpenFilename("Excel Files (*.CSV), *.CSV", Title:="Select File To Be Opened")
        If Ret = False Then Exit Sub
        Set wb = Workbooks.Open(Ret)
        wb.Sheets(active_sheet_name).Range("E21:E2136").Copy NewWB.Sheets(active_sheet_name).Range("C2:C2117") 
        wb.Close SaveChanges:=False

        Set wb = Nothing
        Set NewWB = Nothing
    End With
End Sub

If you still get "Subscript out of range" error, that means that you either don't select correctly the ActiveSheet before running the script, or that sheet does not exist in the workbook wb.

Upvotes: 1

Related Questions