2

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?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
user3764484
  • 51
  • 2
  • 8

1 Answers1

4

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
Comintern
  • 21,855
  • 5
  • 33
  • 80
  • @user3764484 Please [accept](https://stackoverflow.com/help/someone-answers) the answer (and do so for all your old questions). Under the up/down arrows on the left, there is a check mark sign, click on it to accept. – MEE Dec 17 '18 at 20:56