Subscript out of range (Error 9)

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
411
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 to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
411
Office Version
  1. 365
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,606
Office Version
  1. 365
Platform
  1. Windows
Try
VBA Code:
WB.Activate
although it's rarely necessary to select or activate anything.
 

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
411
Office Version
  1. 365
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,606
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Is that the actual name of the sheet as seen on the tab?
 

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
411
Office Version
  1. 365
Platform
  1. Windows
This is copied & pasted from the tab, Sheet1
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,606
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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
 
Solution

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
411
Office Version
  1. 365
Platform
  1. Windows
Works a treat, thanks for taking the time to help me Fluff
Gary
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,606
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,141,777
Messages
5,708,465
Members
421,571
Latest member
ChaosPup

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