dynamic file opening

arian29

Board Regular
Joined
Sep 12, 2011
Messages
64
I need a code in vba which would read a file name in a specified folder (dynamic path). the file name changes every time and there can be more than one files and the code should pick them all. The file is recognised by a word say "QWE" which exists in the file name everytime. e.g. "E2 QWE T556M.XLSX" etc...
icon_confused.gif
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi

Dir is your friend:

Code:
Dim arrFiles As Variant
Dim strFile As STring, pth As String
Dim i As Long

pth = "C:\Somefolder\"

strFile = Dir(pth & "*QWE*")

If strFile<>"" Then
   i = 1
   Do
      ReDim arrFiles(1 To i)
      arrFiles(i) = pth & "strFile"
      strFile = Dir
      i = i + 1
   Loop While strFile<>""
Else
  MsgBox "No file found":Exit Sub
End If

'now use array of files:

For i = 1 To Ubound(arrFiles)
  Workbooks.Open arrFiles(i)
Next i
 
Upvote 0
hi, when the code come to Workbooks.Open arrFiles(i), i get an error > 'D:\strFile.xlsx' could not be found. please check spelling.........
 
Upvote 0
Sorry - code in Do Loop was wrong - should have been this:

Code:
Do
      ReDim arrFiles(1 To i)
      arrFiles(i) = pth & strFile   '=== Note no quotes around strFile!
      strFile = Dir
      i = i + 1
   Loop While strFile<>""
 
Upvote 0
now i am gettin the error.. " could not be found.
i have placed two files this time. if there is one file , the code is working fine but not for more than one files.
 
Upvote 0
Apologies - this should correct:

Code:
Dim arrFiles As Variant
Dim strFile As String, pth As String
Dim i As Long

pth = "D:\"

strFile = Dir(pth & "*QWE*")

If strFile <> "" Then
    ReDim arrFiles(1 To 1)
   i = 1
   Do
    arrFiles(i) = pth & strFile
    strFile = Dir
    If strFile <> "" Then
      i = i + 1
      ReDim Preserve arrFiles(1 To i)
    Else
      Exit Do
    
    End If
   Loop
Else
  MsgBox "No file found": Exit Sub
End If

'now use array of files:

For i = 1 To UBound(arrFiles)
  Workbooks.Open arrFiles(i)
Next i
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top