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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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,275
Messages
6,171,119
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