List excel files in folder but exclude ones with "old" at the beginning

FFischer

Board Regular
Joined
Oct 10, 2013
Messages
59
Please help with solving a macro. I need a routine that will list all Excel files in a specific folder, but exclude the ones that begin with the word "Old". If push comes to shove, I could simply list all files, then use Excel's filtering functionality to accomplish, but I am trying to avoid that workaround and use only as a last resort. Thanks for any guidance that can be offered.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Please help with solving a macro. I need a routine that will list all Excel files in a specific folder, but exclude the ones that begin with the word "Old". If push comes to shove, I could simply list all files, then use Excel's filtering functionality to accomplish, but I am trying to avoid that workaround and use only as a last resort. Thanks for any guidance that can be offered.
After you enter the path to the specific folder where noted below, see if this does what you want.
Code:
Sub ListFilesIfNotOld()
Dim pStr As String, myFile As String, newSht As Worksheet, nR As Long
'Enter your folder path in next line
pStr = "Folder path goes between these quote marks" & Application.PathSeparator
myFile = Dir(pStr & "*.xls*")
If myFile = vbNullString Then Exit Sub
Application.ScreenUpdating = False
Set newSht = Sheets.Add(before:=Sheets(1))
With ActiveSheet
    .Name = "FileList"
    .Range("A1").Value = "Files"
    If Not LCase(Left(myFile, 3)) = "old" Then
        .Range("A2").Value = myFile
        nR = .Range("a" & Rows.Count).End(xlUp).Row + 1
    End If
End With
Do Until myFile = vbNullString
    myFile = Dir()
    If Not LCase(Left(myFile, 3)) = "old" Then
        newSht.Range("A" & nR).Value = myFile
        nR = newSht.Range("a" & Rows.Count).End(xlUp).Row + 1
    End If
Loop
newSht.Columns("A").AutoFit
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,420
Messages
6,124,803
Members
449,190
Latest member
cindykay

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