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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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,216,101
Messages
6,128,835
Members
449,471
Latest member
lachbee

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