Hilmi
Hilmi

Reputation: 43

Run-time error 91 Object Variable or With Block not Set

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

Upvotes: 0

Views: 206

Answers (1)

Keith Stein
Keith Stein

Reputation: 6766

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.

Upvotes: 1

Related Questions