0

I am getting this error

Run Time Error '91' : Object Variable or With Block not Set.

What I am try to do is let user select files then select sheet3 (which name is Raw data) from selected files then copy to the current workbook

My code is

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 OpenBook <> False Then
    Set OpenBook = Application.Workbooks.Open(myFile)
    OpenBook = Application.Workbooks.Open(myFile)
    OpenBook.Sheets(3).Range("A1:3063").Copy
    ThisWorkbook.Worksheets("Raw data(STEP 1)").Range("A2").PasteSpecial xlPasteValues
    OpenBook.Close False
End If

Application.ScreenUpdating = True

End Sub

Highlighted line If OpenBook <> False Then

Any help is appreciated

chris neilsen
  • 52,446
  • 10
  • 84
  • 123
Hilmi
  • 43
  • 9

1 Answers1

1

I think it should be If myFile <> False Then. myFile is the variable you're using to get the file name. OpenBook isn't set until afterwards, hence the null error.

Keith Stein
  • 6,235
  • 4
  • 17
  • 36
  • yeah it works thanks, but this line ```OpenBook = Application.Workbooks.Open(myFile)``` give me error 424 object required is there anything wrongs with the code? – Hilmi Oct 23 '19 at 03:18
  • 1
    The code in your comment is missing `Set`. That would be required. It's there in your question, is it there in your actual code? – Keith Stein Oct 23 '19 at 03:29
  • one more question, Why I am getting Runtime Error '1004' on this line ```OpenBook.Sheets(3).Range("A1:3063").Copy``` – Hilmi Oct 23 '19 at 05:59
  • Hi. In the range u are putting A1:3063.. You missed a letter... – Luis Curado Oct 23 '19 at 06:19