Amend Code to Exclude Files when dialog box Opens

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,570
Office Version
  1. 2021
Platform
  1. Windows
I have the following code below to limit the selection. I need the code below to exclude files containing "ListORefinancing" in the file name for e.g. ListOfRefinancing_Items 8869815.xlsx

it would be appreciated if someone could assist me in amending my code




Code:
Sub SelectFilesand_ConvertToCSV()

Dim strFolderPath As String

Dim selectedFiles() As String

Dim validFiles() As String

Dim selectedFile As Variant

Dim objFD As FileDialog

Dim ws As Worksheet

Dim cell As Range

Dim excludeKeyword As String

Dim fileFilter As String



' Specify the folder path

strFolderPath = "C:\Pull\"

excludeKeyword = "ListOfRefinancing"



' Get the latest modified files matching the specified criteria

selectedFiles = GetLatestFiles(strFolderPath, "ePMz_ListOfgoods_Inquiry", ".xlsx")



If UBound(selectedFiles) = -1 Then

MsgBox "No matching files found.", vbExclamation

Exit Sub

End If



' Reference the worksheet with file names

Set ws = ThisWorkbook.Sheets("Convert")



' Build the list of valid file names from column E

ReDim validFiles(0 To ws.Cells(ws.Rows.Count, "E").End(xlUp).Row - 1)

For Each cell In ws.Range("E1:E" & ws.Cells(ws.Rows.Count, "E").End(xlUp).Row)

validFiles(cell.Row - 1) = cell.value

Next cell



' Create and configure the file dialog

Set objFD = Application.FileDialog(msoFileDialogFilePicker)

With objFD

.AllowMultiSelect = True

.Title = "Select Files"

.InitialFileName = selectedFiles(0) ' Use the first file as the initial file name

.Filters.Clear

.Filters.Add "Excel Files", "*.xls;*.xlsx"



' Filter the selected files based on the valid file names

.FilterIndex = 2 ' Excel Files filter

.InitialFileName = strFolderPath ' Set the initial folder



' Show the file dialog

If .Show = 0 Then Exit Sub



' Process the selected files

For Each selectedFile In .SelectedItems

' Check if the selected file is in the list of valid files

If IsValidFile(selectedFile, validFiles) Then

' Your processing code here for selected files

Debug.Print selectedFile ' Example: Print the selected file path

End If

Next selectedFile

End With



Set objFD = Nothing

End Sub



Function IsValidFile(ByVal filePath As String, ByRef files() As String) As Boolean

Dim file As Variant



For Each file In files

If filePath = file Then

IsValidFile = True

Exit Function

End If

Next file



IsValidFile = False

End Function





Function GetLatestFiles(folderPath As String, fileNamePrefix As String, fileExtension As String) As String()

Dim latestFiles() As String

Dim latestDate As Date

Dim file As String

Dim fileCount As Long



latestDate = DateSerial(1900, 1, 1)



' Count the number of matching files

file = Dir(folderPath & fileNamePrefix & "*" & fileExtension)

Do While file <> ""

If file Like fileNamePrefix & "*" & fileExtension Then

If FileDateTime(folderPath & file) > latestDate Then

latestDate = FileDateTime(folderPath & file)

fileCount = 1

ElseIf FileDateTime(folderPath & file) = latestDate Then

fileCount = fileCount + 1

End If

End If

file = Dir

Loop



' Populate the array with matching files

On Error Resume Next



ReDim latestFiles(0 To fileCount - 1)

file = Dir(folderPath & fileNamePrefix & "*" & fileExtension)

Do While file <> ""

If file Like fileNamePrefix & "*" & fileExtension And FileDateTime(folderPath & file) = latestDate Then

latestFiles(fileCount - 1) = folderPath & file

fileCount = fileCount - 1

End If

file = Dir

Loop



GetLatestFiles = latestFiles

End Function
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
What about this? (not tested).
VBA Code:
        ' Process the selected files
        For Each selectedFile In .SelectedItems
            ' Check if the selected file is in the list of valid files
            If IsValidFile(selectedFile, validFiles) Then
                If InStr(selectedFile, excludeKeyword) = 0 Then
                    ' Your processing code here for selected files
                    
                    
                    
                    Debug.Print selectedFile ' Example: Print the selected file path
                End If
            End If
        Next selectedFile
 
Upvote 0
Solution
Thanks for the help, much appreciated
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,947
Members
449,480
Latest member
yesitisasport

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