+ Reply to Thread
Results 1 to 8 of 8

VBA - User prompt to determine path to files

Hybrid View

  1. #1
    Registered User
    Join Date
    03-08-2013
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    4

    VBA - User prompt to determine path to files

    Sub ProcessListUpdate()


    Const strFolderPath As String = "C:\exceltest\"


    How to create VBA code to create popup window/dialog box to determine path to location of desired .xls files to be edited? Instead of users updating VBA code

    Good resource (book,website) beside excel help forum to learn basic VBA programming? Feeling guilty for taking the easy way out on this..

  2. #2
    Forum Contributor
    Join Date
    07-16-2012
    Location
    Ontario Canada
    MS-Off Ver
    Excel 2010
    Posts
    124

    Re: VBA - User prompt to determine path to files

    Try this

    Sub GetFilePath()
    Dim fn As String
    fn = Application.GetOpenFilename
    MsgBox (fn)
    End Sub

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA - User prompt to determine path to files

    michaelisk,

    Here is some commented code for how to accomplish this. Note that this is not the only way to accomplish this.
    Sub tgr()
        
        Dim oShell As Object
        Dim strFolderPath As String
        
        Set oShell = CreateObject("Shell.Application")
        
        On Error Resume Next    'Prevent error if user presses cancel
        'Prompt user to select a folder
        strFolderPath = oShell.BrowseForFolder(0, "Select Folder", 0).Self.Path & Application.PathSeparator
        Set oShell = Nothing    'Release the memory used for the object variable
        On Error GoTo 0         'Remove the "On Error Resume Next" condition
        
        If Len(strFolderPath) = 0 Then Exit Sub 'User pressed cancel, exit macro
        
        MsgBox strFolderPath
        
    End Sub


    As for book/website for learning, this thread has several useful links: http://www.excelforum.com/excel-prog...materials.html
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,719

    Re: VBA - User prompt to determine path to files

    I think that each of us is reading the issue differently.

    Dim strFolderPath As String
    
    Set strFolderPath = Application.InputBox(prompt:="Enter Path", Type:=2)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA - User prompt to determine path to files

    Quote Originally Posted by alansidman View Post
    I think that each of us is reading the issue differently.
    Quoted For Truth

  6. #6
    Forum Contributor
    Join Date
    07-16-2012
    Location
    Ontario Canada
    MS-Off Ver
    Excel 2010
    Posts
    124

    Re: VBA - User prompt to determine path to files

    I dont mean to hijack someone else's thread but thank you tigeravatar.
    I have been looking for something like that for a long time.

  7. #7
    Registered User
    Join Date
    03-08-2013
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA - User prompt to determine path to files

    Tiger that works great. Just would like to be able to keep the memory of last folder selected for ease of browsing.

    Set oShell = Nothing to ??

  8. #8
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA - User prompt to determine path to files

    michaelisk, I'm not sure what you mean. The chosen folder is stored in the string variable strFolderPath, there is no reason to keep memory reserved for the oShell object variable.

    @jason_lee_91, you're very welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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