Within Excel, use Windows Explorer to locate and paste a file path into cell A2

jase71ds

Board Regular
Joined
Oct 23, 2006
Messages
137
Office Version
  1. 365
Platform
  1. Windows
In another thread I asked about locating and pasting a folder path by using a Windows explorer type dialogue box.
gpeacock gave me this excellent code that works perfectly...
Code:
Sub GetFolder()
    Dim fdg As FileDialog
    Dim ifn As String
       With Application
           Set fdg = .FileDialog(msoFileDialogFolderPicker)
           ifn = .DefaultFilePath & .PathSeparator
       End With
       With fdg
          .InitialFileName = ifn
          .Title = "Select a location"
          .Show
       End With
    If fdg.SelectedItems.Count = 0 Then Exit Sub
    Range("A1").Value = fdg.SelectedItems(1)
 End Sub

But now, in addition to a folder, I also need the user to locate a file using a Windows explorer type dialogue box. The path of the file that the user locates needs to be pasted into A2.

I had thought I would be able to modify the above code to make finding a file (as opposed to a folder) and pasting the file path into A2 -- but alas, my VBA skills are just not there yet.

Could anyone assist?
Please note: I do not want to actually open the file that the user selects. Only paste the URL path into A2.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
This will return the full path (i.e path and filename and extension) as well as just the path. Not sure which you want in A2 but you can easily adjust the code to place either in A2.
Code:
Sub FilePath()
Dim sFullPath As String, sPath As String
sFullPath = Application.GetOpenFilename
sPath = Mid(sFullPath, 1, InStrRev(sFullPath, "\") - 1)
Range("A2").Value = sPath
End Sub
 
Upvote 0
Joe,
This is great!!
I modified it (as you said) to put:
sPath in A1
sFullPath in A2

Lastly, can you help me get just the file name in A3?
I'd be very grateful.

This is the code I have modified ever so slightly from you:
Code:
Sub FilePath()
Dim sFullPath As String, sPath As String
sFullPath = Application.GetOpenFilename
sPath = Mid(sFullPath, 1, InStrRev(sFullPath, "\") - 1)
Range("A1").Value = sPath
Range("A2").Value = sFullPath
End Sub
 
Upvote 0
Joe,
This is great!!
I modified it (as you said) to put:
sPath in A1
sFullPath in A2

Lastly, can you help me get just the file name in A3?
I'd be very grateful.

This places the file name (including file extension) in A3 of the activesheet:
Code:
Sub FilePath()
Dim sFullPath As String, sPath As String
sFullPath = Application.GetOpenFilename
sPath = Mid(sFullPath, 1, InStrRev(sFullPath, "\") - 1)
Range("A1").Value = sPath
Range("A2").Value = sFullPath
Range("A3").Value = Right(sFullPath, Len(sFullPath) - InStrRev(sFullPath, "\"))
End Sub
 
Upvote 0
Joe,

Thank you so much! I'll GLADLY buy you a beer if you ever come to Houston.
Your code does everything I need.

Again, thank you so very much!

Jase.
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,298
Members
449,077
Latest member
Rkmenon

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