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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,276
Messages
6,171,140
Members
452,381
Latest member
Nova88

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