VBA filepicker, file name only.

bmpreston

Board Regular
Joined
Jun 18, 2016
Messages
120
Office Version
  1. 365
Platform
  1. MacOS
I have a VBA script found on this site which ironically worked, and then it stopped...

I admittedly do not know how this works, as I can't barely parse out VBA, but the purpose of this is to find just the file name for a msofilepicker that runs. When I then value a cell to the variable fname, I get the entire filepicker name AND location.

Any assistance?

Thanks

VBA Code:
Sub ChooseFile()

    Dim fd As FileDialog
    Dim fName As String  ' Includes full path
    Dim fChosen As Integer
    Dim fNameFile As String  'Only the name of the file
   
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
            fd.Title = "Please select file"
            fd.InitialFileName = "C:\Users\xx\Downloads\CSVdata*"
            fChosen = fd.Show
                    fd.Filters.Clear
                    fd.Filters.Add "CSV files", "*.csv"
    
         If fChosen <> -1 Then
             MsgBox "You Cancelled, nothing done"
         Else
            fName = Right$(fd.SelectedItems(1), Len(fd.SelectedItems(1)) - InStrRev(fd.SelectedItems(1), "\"))
range("t5").value = fName
        End If

End Sub
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Seems to me that your macro is in working condition, it deliveres in cell T5 only the name of the file.
Anyway, I would make some changes to fix what I think is a typo and to have the FileDialog showing only .csv files.
VBA Code:
'...
fd.InitialFileName = "C:\Users\xx\Downloads\CSVdata\" '<= was ..\CSVdata*"
fd.Filters.Clear
fd.Filters.Add "CSV files", "*.csv"
fChosen = fd.Show                             '<= moved down
'...
 
Upvote 0
Seems to me that your macro is in working condition, it deliveres in cell T5 only the name of the file.
Anyway, I would make some changes to fix what I think is a typo and to have it showing only .csv files.
VBA Code:
'...
fd.InitialFileName = "C:\Users\xx\Downloads\CSVdata\" '<= was ..\CSVdata*"
fd.Filters.Clear
fd.Filters.Add "CSV files", "*.csv"
fChosen = fd.Show                             '<= moved down
'...
I can agree. I had to comment out some of it, I didn’t post it as I have it. I posted as I found it in the forums.
But strangely in my workbook, I get the entire file name. (Folders and all).
Thanks
 
Upvote 0
No other idea at the moment, not sure but if you are using a Mac maybe you should be using "/" instead of "\" in this line of code:
fName = Right$(fd.SelectedItems(1), Len(fd.SelectedItems(1)) - InStrRev(fd.SelectedItems(1), "\"))
 
Upvote 0
No other idea at the moment, not sure but if you are using a Mac maybe you should be using "/" instead of "\" in this line of code:
fName = Right$(fd.SelectedItems(1), Len(fd.SelectedItems(1)) - InStrRev(fd.SelectedItems(1), "\"))
Ironically, you nailed it. It's not a Mac, however I'm using a Sharepoint site. teams.foobar.com/sites/sharepoint/asdfasdf/asdfasdf/asdfasdf.xlsm

Thank you!
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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