excelvbacopyrangegetopenfilename

Getting Runtime Error 1004 using Copy Range method


Description: What am I trying to do is allow user to select excel file via browse then copy data from Sheet3 in selected file and paste to current workbook Sheet2 (which name is Raw data(STEP 1)).

My problem is that I am getting:

runtime error 1004 application-defined or object-defined error

on this line ActiveSheet.Range("A2:3063").Copy

My full code:

Private Sub OpenWorkBook_Click()

Dim myFile As Variant
Dim OpenBook As Workbook
Application.ScreenUpdating = False

myFile = Application.GetOpenFilename(Title:="Browse your file", FileFilter:="Excel Files(*.xls*),*xls*")

If myFile <> False Then
    Set OpenBook = Application.Workbooks.Open(myFile)
    OpenBook.Sheets(3).Activate
    ActiveSheet.Range("A2:3063").Copy
    ThisWorkbook.Worksheets("Raw data(STEP 1)").Range("A3").PasteSpecial xlPasteValues
    OpenBook.Close True
End If

Application.ScreenUpdating = True

End Sub

Solution

  • Your range address is invalid. Probably you mean ActiveSheet.Range("A2:A3063")

    And you should read https://stackoverflow.com/a/10717999/7599798 to get an idea how to deal with Sheets and Ranges without using Activate (or Select)