1

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,000
  • 34
  • 174
  • 243
user3764484
  • 41
  • 2
  • 7

1 Answers1

3

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,247
  • 5
  • 32
  • 78
  • @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