VBA

Rawan_7

New Member
Joined
Nov 15, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello, I need assistance with VBA. How to Open the dialog box and gets a file name from the user without actually opening any files.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
msoFileDialogFilePicker will return file name but not open it (unless you add code to do that). Add the Sub name and End Sub lines you need.
VBA Code:
Dim fd

Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
   .Title = "Select File"
   .AllowMultiSelect = False
   .InitialFileName = "C:\Users\Micron\Documents\Excel\A*"
   .Filters.Add "EXCEL", "*.xlsm", 1
End With
If fd.Show = -1 Then Workbooks.Open fd.SelectedItems(1) 'alter this line to not open the file

Set fd = Nothing
If you don't want users to be able to nmulti select, leave that as False else you will have to alter code to loop over selections.
If you don't want to provide the starting point, remove the InitialFileName line. Filters property should seem obvious to you.
You should have If fd.Show = -1 Then (-1 means a selection was made) so that you can handle the situation where the user cancels. You need a reference to Microsoft Office if I recall correctly.
 
Upvote 0
Look up the GetOpenFilename Method

VBA Code:
Sub example()
    Dim sFileName As String
   
    sFileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xlsx")
    If sFileName <> "False" Then
       MsgBox "You chose : '" & sFileName & "'"
    Else
       MsgBox "You cancelled the operation."
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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