zifan yan
zifan yan

Reputation: 125

vba application.workbooks.open() Error 1004 Application-defined or Object-defined error

Sub test()
   Name = "C:\Users\zif\Desktop\CAT\as1_new\tt.xlsx"
   wb = Application.Workbooks.Open("C:\Users\zif\Desktop\CAT\as1_new\tt.xlsx")
End Sub

this is my code and i just want to test it. I am sure the directory and filename is correct, and the file is not opened, but i still have this error


update: Thank you for answering my questions I have changed my code to below:

Sub test()
    Dim wb As Workbook
    myName = "C:\Users\zif\Desktop\CAT\as1_new\tt.xlsx"
    Set wb=Application.Workbooks.Open("C:\Users\zif\Desktop\CAT\as1_new\tt.xlsx")
End Sub

but it still throws the same error message to me.

Upvotes: 2

Views: 654

Answers (1)

riskypenguin
riskypenguin

Reputation: 2199

I think you'll have to initialize wb first, like this:

Dim wb As Workbook

When assigning th object you need to use the Setstatement, like this:

Set wb = Application.Workbooks.Open("C:\Users\zif\Desktop\CAT\as1_new\tt.xlsx")

Edit: There is a lot of good additional advice in the comments to the question, which should be considered.

Upvotes: 2

Related Questions