+ Reply to Thread
Results 1 to 8 of 8

VBA code to get file path

Hybrid View

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

    VBA code to get file path

    Hi Team,

    I am looking for a VBA code that would ask to select a file and then give its location path in excel column F.

    Thanks,
    Liz.
    Last edited by Liz_Biz; 10-03-2014 at 11:47 AM.

  2. #2
    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...

  3. #3
    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

  4. #4
    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

    I don't understand your reply, liz.

    1) Are you saying you WANT to allow multiple file selections, and write them all into Column F?
    2) I have no idea what you mean. Please explain.
    3. Simply change this line:
    ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).Value = Left(stFile, InStrRev(stFile, "\") - 1)
    to
    ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).Value = stFile

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

    Re: VBA code to get file path

    1) Yes. Its correct.
    2) Its working by solution of 3rd point.
    Last edited by Liz_Biz; 10-03-2014 at 11:19 AM.

  6. #6
    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

    Okay, here's a version which allows multiple selections:
    Sub fooFile()
    Dim i As Integer
    On Error Resume Next
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = True
        .Show
        If Not .SelectedItems.Count = 0 Then
            For i = 1 To .SelectedItems.Count
                ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).Value = .SelectedItems(i)
            Next i
        End If
    End With
    End Sub

  7. #7
    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 Olly. Your are great guy.

    Liz.

  8. #8
    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

    Happy to help, thanks for the feedback

+ 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