I need my code below to limit the files to browse in a folder to BR1 Southern Region SalesLedgerOutstandingTransactions
Kindly amend my code
Kindly amend my code
Code:
Sub Open_Workbook()
Dim nb As Workbook, tw As Workbook, ts As Worksheet
Dim selectedFilePath As String
Dim fso As Object
Dim folderPath As String
' Set the default folder for opening files
folderPath = "C:\extract"
' Create a FileDialog object to select a file
Set fso = CreateObject("Scripting.FileSystemObject")
selectedFilePath = FilePicker(fso, folderPath)
' Check if the selected file has the desired name
If Not fso.GetBaseName(selectedFilePath) = BR1 Southern Region SalesLedgerOutstandingTransactions" Then
MsgBox "Please select the fileBR1 Southern Region SalesLedgerOutstandingTransactions.csv", vbExclamation
Exit Sub
End If
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set nb = Workbooks.Open(fileName:=selectedFilePath, Local:=True)
' Copy data from the new sheet to "Debtors" in the original workbook
nb.Sheets(1).UsedRange.Copy Destination:=tw.Sheets("Debtors").Cells(1, 1)
' Close the opened workbook without saving changes
nb.Close SaveChanges:=False
ts.Activate
With Application
.CutCopyMode = False ' Clear the clipboard
.ScreenUpdating = True
End With
End Sub
Function FilePicker(fso As Object, folderPath As String) As String
Dim fileName As String
Dim fd As Object
' Create a FileDialog object to select a file
Set fd = Application.fileDialog(3) ' 3 corresponds to msoFileDialogFilePicker
With fd
.Filters.Clear
.Filters.Add "CSV Files", "*.csv"
.FilterIndex = 1
.InitialFileName = folderPath & "\" & "BR1 Southern Region SalesLedgerOutstandingTransactions.csv"
.Title = "Select CSV file"
If .Show = 0 Then Exit Function ' User canceled the dialog
fileName = .SelectedItems(1)
End With
FilePicker = fileName
End Function