user3764484
user3764484

Reputation: 51

excel vba open workbook

My code is:

Dim wb As Workbook
Dim MyObj As Object, MySource As Object, file As Variant
file = Dir("C:\Users\dlf164\Desktop\NE\")
While (file <> "")
    If InStr(file, a) > 0 Then
        Set wb = Workbooks.Open(file)

    End If
file = Dir
Wend

The error which I am receiving is Application or object defined runtime error.

How to solve this?

Upvotes: 2

Views: 4733

Answers (1)

Comintern
Comintern

Reputation: 22185

Dir() only returns the filename, but Workbooks.Open() requires the full path. Try something like this:

Dim wb As Workbook
Dim MyObj As Object, MySource As Object, file As Variant

Dim path As String
path = "C:\Users\dlf164\Desktop\NE\"
file = Dir(path)
While (file <> "")
    If InStr(file, a) > 0 Then
        Set wb = Workbooks.Open(path & file)
    End If
file = Dir
Wend

Upvotes: 4

Related Questions