Paste file name and extension using file explorer

Ace71425

Board Regular
Joined
Apr 20, 2015
Messages
130
See code below....

Code:
Sub GetFilePath()Set myFile = Application.FileDialog(msoFileDialogOpen)
With myFile
    .Title = "Choose File"
    .AllowMultiSelect = True
    If .Show <> -1 Then
        Exit Sub
    End If
    For I = 1 To .SelectedItems.Count
        Range("K" & Rows.Count).End(xlUp).Offset(1) = .SelectedItems(I)
    Next I
End With
End Sub

I have this code to open a file explorer and let you select one or more files and then paste the file path to the next available K cell. I want the code to post just the file name and not the full path. How would I do this??? Thanks in advance!!!! PS I would like it to paste the file extension too...So file name and file extension
 

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.
Maybe this :
Code:
Sub GetFilePath()
Set myFile = Application.FileDialog(msoFileDialogOpen)
With myFile
    .Title = "Choose File"
    .AllowMultiSelect = True
    If .Show <> -1 Then
        Exit Sub
    End If
    For I = 1 To .SelectedItems.Count
        Range("K" & Rows.Count).End(xlUp).Offset(1) = Right(.SelectedItems(I), Len(.SelectedItems(I)) - InStrRev(.SelectedItems(I), "\"))
    Next I
End With
End Sub
 
Upvote 0
See below. Inside the For loop I put the full file name into a string variable then used the Dir() function to return the file name and extension only(without the path).

Code:
Sub GetFilePath()

Set myFile = Application.FileDialog(msoFileDialogOpen)

With myFile
  .Title = "Choose File"
  .AllowMultiSelect = True
  If .Show <> -1 Then
    Exit Sub
  End If
  For I = 1 To .SelectedItems.Count
    s=.SelectedItems(I)
    Range("K" & Rows.Count).End(xlUp).Offset(1) = Dir(s)
  Next I
End With

End Sub

Hope this helps.
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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