+ Reply to Thread
Results 1 to 8 of 8

VBA code to get file path

Hybrid View

  1. #1
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA code to get file path

    This opens a file picker dialog, then writes the path of the selected file into the next empty cell in column F:
    Sub fooFile()
    Dim stFile As String
    On Error Resume Next
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Show
        stFile = .SelectedItems(1)
    End With
    If Not stFile = "False" Then
        ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).Value = Left(stFile, InStrRev(stFile, "\") - 1)
    End If
    End Sub
    Alternatively, instead of browsing for a specific file, you can just pick a folder:
    Sub fooFolder()
    Dim stFolder As String
    On Error Resume Next
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        stFolder = .SelectedItems(1)
    End With
    If Not stFolder = "False" Then
        ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).Value = stFolder
    End If
    End Sub
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  2. #2
    Registered User
    Join Date
    12-19-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    53

    Re: VBA code to get file path

    Thank you so much Olly for looking into this,

    Below code is useful but need some modifications.

    1) File dialog could select multiple files by using ctrl.
    2) Add data from last available cell , not always 2nd one.
    3) Path should contain complete path as "C:\Documents and Settings\Liz\Desktop\ABC.xlsx"

    Sub fooFile()
    Dim stFile As String
    On Error Resume Next
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Show
        stFile = .SelectedItems(1)
    End With
    If Not stFile = "False" Then
        ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).Value = Left(stFile, InStrRev(stFile, "\") - 1)
    End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA Code to get file names with path
    By Liz_Biz in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 06-23-2014, 10:38 PM
  2. Need code for hyperlink file path
    By DraftRanger in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2014, 01:07 PM
  3. Code to list the folder path and sub folder path of a specific file
    By kalai1587 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-13-2013, 03:51 AM
  4. Help with code: amend file path
    By jp84 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-15-2013, 11:00 PM
  5. code relative file path?
    By Glio in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-09-2007, 03:02 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1