Loop through folder and find files with specific name

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,421
Office Version
  1. 2016
Platform
  1. Windows
I need to loop through a specific folder and find all files that have the phrase '(Full)' in the filename.

If it finds I file I need it to run some code that's yet to be written.

I've searched the net and can't find anything that does what I though would be a simple task - can anyone help?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
This is a generic structure for looping through a directory for files. Note that 'Full' has an asterisk before and after, If Full will always be the fist word of the file name, remove the first asterisk, esle those files will not be returned. Same if the word 'Full' is always the last word of the file name, remove the asterisk after the word 'Full'. The asterisk is a wildcard for data that should be there. VBA then expects something to be there if the asterisk is used. If the asterisk is there but the file has no data there, then VBA sees that as a mismatch and ignores it. So the asterisks should be used to represent the configuration of your file names. The code below assumes data preceding and following the word 'Full'.

Code:
Sub getFiles()
Dim fName As String, fPath As String, wb As Workbook
fPath = ThisWorkbook.Path 'substitute actual path is not in same directory as host workbook.
If Right(fPath, 1) <> "\" Then fPath = fPath & "\"
fName = Dir(fPath & "*Full*.xls*")
	Do While fName <> ""
		Set wb = Workbooks.Open fPath & fName
		'enter yet to be written code here
		wb.Close 'True or False to either save or not save workbook after code runs.
		fName = Dir
	Loop
End Sub
 
Last edited:
Upvote 0
Hi, and thanks - however, the following line is highlighted in red and I don't know why;

Code:
Set wb = Workbooks.Open fPath & fName
 
Upvote 0
Hi, and thanks - however, the following line is highlighted in red and I don't know why;

Code:
Set wb = Workbooks.Open fPath & fName

Should be:
Code:
Set wb = Workbooks.Open(fPath & fName)
Parentheses are needed when the equal sign is used to set a varible. My oversight.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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