opening up specific files in a folder

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have written code to open up all the files in C:\Sales Reports containing the text "OBS"

when running the macro, nothing happens


kindly amend my code so that all files containing text OBS for eg CT Sales Obsolesence Jan 2018, BT Sales Obsolesense for Jan 2018 etc is opened


Code:
 Sub OpenFiles()
Dim MyFolder As String
Dim MyFile As String
MyFolder = "C:\Sale Reports"
MyFile = Dir(MyFolder & "\*obs*.xls")
Do While MyFile <> ""
    Workbooks.Open Filename:=MyFolder & "\" & MyFile
Loop
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi,

This should do it. Your code worked fine to open the first file, but then stopped as it was always choosing the first file.
change the directory to whatever you require, but it now works fine for me.

Code:
Sub OpenFiles()
Dim MyFolder As String
Dim MyFile As String
Dim a As Workbook
MyFolder = "C:\test"
MyFile = Dir(MyFolder & "\*obs*.xls")
Do While MyFile <> ""
    Set a = Workbooks.Open(Filename:=MyFolder & "\" & MyFile)
    MyFile = Dir
    Loop
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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