Macro to limit browsing to specific File

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I need my code below to limit the files to browse in a folder to BR1 Southern Region SalesLedgerOutstandingTransactions


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
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
When the Code browses to the folder I will to limit the files to csv files like "SalesLedgerOutstandingTransactions" so it makes it easier for the user to select the file
 
Upvote 0
I finally managed to sort out the code-See updated Code below

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
Dim FilePath As String
Dim FD As Object
    Set FD = Application.fileDialog(3) ' 3 corresponds to msoFileDialogFilePicker
  
    FD.InitialFileName = "C:\Extract\"


    ' Display a message box before selecting a file
    MsgBox "Please select Sales Ledger file for this branch.", vbInformation

    With FD
        .Filters.Add "CSV", "*.csv"
        .FilterIndex = 1
        .InitialFileName = "C:\Extract\*Salesledger*.csv"
        .Title = "Select CSV File"
        If .Show = 0 Then Exit Sub
        fileName = .SelectedItems(1)
    End With
    Debug.Print fileName

    Set tw = ThisWorkbook
    Set ts = tw.ActiveSheet
    Set nb = Workbooks.Open(fileName:=fileName, Local:=True)
  
    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
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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