Error Running Copy VBA Code on Workbook_Open

Bentley001

New Member
Joined
May 12, 2015
Messages
16
I'm trying to run the code below to copy updated data from another file but am getting a 'Subscript out of Range' error on the line where it is referencing the tab on the source file (Sheets("Source File Tab").Select). The code runs fine when attached to a macro driven button. Confused on why it's not working?

Private Sub Workbook_Open()

Application.ScreenUpdating = False
Application.CutCopyMode = False
Application.DisplayAlerts = False

ThisWorkbook.Activate
Sheets(4).Select
Sheets(4).Cells.ClearContents

Workbooks.Open Filename:="Path to Source File.xls", ReadOnly:=True

Workbooks("Source File Name.xls").Activate
Sheets("Source File Tab").Select
[a1].CurrentRegion.Copy

ThisWorkbook.Activate

[D1].PasteSpecial Paste:=xlPasteValues
[a1].Select

Workbooks("Source File Name.xls").Close

Application.ScreenUpdating = True
Application.DisplayAlerts = True
Sheets("Sheet1").Select

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi,
try running code from a standard module.

not tested but see if this update helps:

standard module:
Rich (BB code):
Sub CopyData()
    Dim wbSource As Workbook
    Dim wsDest As Worksheet
    
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
    
    On Error GoTo myerror
    Set wsDest = ThisWorkbook.Sheets(4)
    
    wsDest.Cells.ClearContents
    
    Set wbSource = Workbooks.Open(Filename:="Path to Source File.xls", ReadOnly:=True)
    
    wbSource.Sheets("Source File Tab").Range("A1").CurrentRegion.Copy
    
    
    wsDest.Range("D1").PasteSpecial Paste:=xlPasteValues
    
    wbSource.Close False
  
myerror:
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
        .CutCopyMode = False
    End With
    
    If Err > 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Thisworkbook Code Page:

Rich (BB code):
Private Sub Workbook_Open()
    CopyData
End Sub

You will need to ensure that the Path / File name & sheet name shown in RED are valid - update as required.

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,206,710
Messages
6,074,457
Members
446,071
Latest member
Jolon

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