Subscript Out Of Range Error 9

excelbytes

Active Member
Joined
Dec 11, 2014
Messages
251
Office Version
  1. 365
Platform
  1. Windows
I wrote the following code which works perfectly:

VBA Code:
Sub CopyData()
'
' CopyData Macro

    Dim WB As Workbook
    Dim WBDest As Workbook
    Dim WS As Worksheet
    
    Set WB = Application.Workbooks("2023 HR Monthly Report - Gramalote.xlsm")
    Set WBDest = Application.Workbooks("Consolidated HR Report.xlsm")
    
    WB.Activate
    Sheets("Gr_L_Headcount").Range("A8:AO53").Copy
    WBDest.Worksheets("Headcount").Range("A170").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    WB.Activate
    Sheets("Gr_L_Training").Range("C7:N29").Copy
    WBDest.Worksheets("Training").Range("C91").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    WB.Activate
    Sheets("Gr_L_Employee Turnover").Range("AL4:AL13").Copy
    WBDest.Worksheets("Employee Turnover").Range("AU19").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    WB.Activate
    Sheets("Gr_L_Employee Turnover").Range("AN4:AN13").Copy
    WBDest.Worksheets("Employee Turnover").Range("AU34").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    WB.Activate
    Sheets("Gr_L_Employee Categories").Range("D3:G20").Copy
    WBDest.Worksheets("Categories").Range("P6").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    WB.Activate
    Sheets("Headcount Definitions").Range("A1").Select

End Sub

I copied it to another workbook and made only the changes to the WB name, sheet names and range references:

VBA Code:
Sub CopyData()
'
' CopyData Macro

    Dim WB As Workbook
    Dim WBDest As Workbook
    Dim WS As Worksheet
    
    Set WB = Application.Workbooks("2023 HR Monthly Report - Columbia EXPATS.xlsm")
    Set WBDest = Application.Workbooks("Consolidated HR Report.xlsm")
    
    WB.Activate
    Sheets("Co_X_Headcount").Range("A8:AO53").Copy
    WBDest.Worksheets("Headcount").Range("AQ64").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    WB.Activate
    Sheets("Co_X_Training").Range("C7:N29").Copy
    WBDest.Worksheets("Training").Range("R35").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    WB.Activate
    Sheets("Co_X_Employee Turnover").Range("AL4:AL13").Copy
    WBDest.Worksheets("Employee Turnover").Range("CH19").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    WB.Activate
    Sheets("Co_X_Employee Turnover").Range("AN4:AN13").Copy
    WBDest.Worksheets("Employee Turnover").Range("CH34").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    WB.Activate
    Sheets("Co_X_Employee Categories").Range("D3:G20").Copy
    WBDest.Worksheets("Categories").Range("H33").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    WB.Activate
    Sheets("Headcount Definitions").Range("A1").Select

End Sub

But I'm getting a "Subscript Out Of Range Error 9" on this line:

Set WB = Application.Workbooks("2023 HR Monthly Report - Columbia EXPATS.xlsm")

What am I doing wrong?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Is that that EXACT workbook name (check for typos, extra characters, etc) and is it open in that session of Excel?
 
Upvote 0
Solution
Is that that EXACT workbook name (check for typos, extra characters, etc) and is it open in that session of Excel?
I double checked it and it looked right. So now I copied it and pasted it from another source, don't see any changes, but now it works. I don't know, but there must have been some difference. Thanks.
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,038
Members
449,092
Latest member
ikke

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