Cat On A Spreadsheet

Cat On A Spreadsheet
Dim strPassword as String 'this will hold the password to open the file, if any
Dim varSrcPath as Variant 'this will hold the file path
Dim wbSrc as Workbook 'this is the workbook variable
'browse for source file
varSrcPath = Application.GetOpenFilename(FileFilter:="Csv files (*.csv), *.csv", Title:="Select Source File")
'check if no file was selected
if varSrcPath = False then
          MsgBox "No file selected."
          GoTo Cleanup
Else:
          'if a file was selected, open it
          Set wbSrc = Workbooks.Open(Filename:=varSrcPath, Password:=strPassword)
          'handler for wrong password error
          If Err.Number <> 0 Then
                    Err.Clear
                    MsgBox "Wrong Password." & strPassword
                    GoTo Cleanup
          End If
          'end error trap
          On Error GoTo 0
End If
07 April 2025

Excel VBA - code to browse for a file

Hi, and welcome back. This is going to be just a short post to show you one way of opening an explorer window which allows you to navigate to and select a file. If no file is selected or if the password is incorrect, the code skips ahead to the Cleanup section in our project.

Full Service Consulting

Reporting

Automation

Cat On A Spreadsheet

Cat On A Spreadsheet