Subscript out of range (Error 9)

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
467
Office Version
  1. 365
Platform
  1. Windows
Can anybody please tell me why I am getting the error “Run time Error 9 Subscript out of range” on this line of my code.
VBA Code:
Windows("Approved Suppliers List.xlsm").Activate
The workbook “Approved Suppliers List” opens up OK just cannot seem to activate it
I have even recorded it to make sure all the workbook names are correct, but still the error, Full code below
Any help is very much appreciated
Code:
Private Sub Workbook_Open()
Dim WB As Workbook, wkbk As Workbook
Dim CurrentSheet As Worksheet
Set CurrentSheet = ActiveSheet
Set wkbk = ActiveWorkbook
'Set WB = Workbooks.Open("\\192.168.55.50\waste\Operational Data\_Databases\Approved Suppliers List.xlsm", True, True)

'Application.ScreenUpdating = False ' turn off the screen updating
      
        Windows("Purchase Order list.xlsm").Activate
            Sheets("Suppliers").Select
        Range("A1").Select
On Error GoTo 0
    Range(Selection, Selection.End(xlDown)).ClearContents
0
    Windows("Approved Suppliers List.xlsm").Activate
        Sheets("Sheet1").Select
            Range("B4").Select
        Range(Selection, Selection.End(xlDown)).Copy
        
    Windows("Purchase order List.xlsm").Activate
        Sheets("Suppliers").Select
            Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Sheets("Page 1").Select
    
'    Application.CutCopyMode = False
'       WB.Close savechanges:=False ' close the source workbook (Approved suppliers List) without saving any changes
'            Wkbk.Activate
'    Application.ScreenUpdating = True ' turn on the screen updating
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Apologies I see I had a line I left commented out, please find full code below
VBA Code:
Private Sub Workbook_Open()
Dim WB As Workbook, wkbk As Workbook
Dim CurrentSheet As Worksheet
Set CurrentSheet = ActiveSheet
Set wkbk = ActiveWorkbook
Set WB = Workbooks.Open("\\192.168.55.50\waste\Operational Data\_Databases\Approved Suppliers List.xlsm", True, True)

'Application.ScreenUpdating = False ' turn off the screen updating
      
        Windows("Purchase Order list.xlsm").Activate
            Sheets("Suppliers").Select
        Range("A1").Select
On Error GoTo 0
    Range(Selection, Selection.End(xlDown)).ClearContents
0
    Windows("Approved Suppliers List.xlsm").Activate
        Sheets("Sheet1").Select
            Range("B4").Select
        Range(Selection, Selection.End(xlDown)).Copy
        
    Windows("Purchase order List.xlsm").Activate
        Sheets("Suppliers").Select
            Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Sheets("Page 1").Select
    
'    Application.CutCopyMode = False
'       WB.Close savechanges:=False ' close the source workbook (Approved suppliers List) without saving any changes
'            Wkbk.Activate
'    Application.ScreenUpdating = True ' turn on the screen updating
End Sub
 
Upvote 0
Try
VBA Code:
WB.Activate
although it's rarely necessary to select or activate anything.
 
Upvote 0
Thanks Fluff that worked great but I then get the same error 9 on this line
VBA Code:
 Sheets("Sheet1").Select
I know the sheet name is correct
 
Upvote 0
Is that the actual name of the sheet as seen on the tab?
 
Upvote 0
This is copied & pasted from the tab, Sheet1
 
Upvote 0
Instead of
VBA Code:
    Windows("Approved Suppliers List.xlsm").Activate
        Sheets("Sheet1").Select
            Range("B4").Select
        Range(Selection, Selection.End(xlDown)).Copy
try
VBA Code:
        With WB.Sheets("Sheet1")
            .Range("B4", .Range("B4").End(xlDown)).Copy
        End With
 
Upvote 0
Solution
Works a treat, thanks for taking the time to help me Fluff
Gary
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,185
Members
448,554
Latest member
Gleisner2

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