+ Reply to Thread
Results 1 to 2 of 2

I need a macro to open a file, but I don't want to hard code the path.

Hybrid View

  1. #1
    Registered User
    Join Date
    04-21-2016
    Location
    Minneapolis, Minnesota
    MS-Off Ver
    2016
    Posts
    26

    I need a macro to open a file, but I don't want to hard code the path.

    I have a Summary workbook the pulls data from new workers' logs via a macro called "Updateme"

    Currently, I need to update the "Updateme" macro in the summary work book for each hire group with the path to the directory where their training logs are located.

    I will be promoting out of my current position and will be handing the duty of maintaining these logs off to a co-worker.

    I want to code the updater so that the path to the logs does not need to be hard coded, but is variable as each hire group gets its own folder. My initial thought was to code a field in List.xlsx with the path listed for each worker, but then I realized that the path to List.xlsx is what I need to make indeterminate!

    I had the idea to use "Application.GetOpenFilename("*.xlsx") but I don't want the user to have to find the list.xlsx file every time.

    Is it possible to have the macro pull the directory from "Application.GetOpenFilename("*.xlsx") using something like:
    Dim sFullName As String
        Dim sFileName As String
    
        sFullName = Application.GetOpenFilename("*.txt,*.txt")
        sFileName = Dir(sFullName)
    and then have the macro write the directory or path in the macro for the future, once the path is known?

    Any ideas on how to do this?

    The relevant part of my existing macro is below.

    WorkbookPath = "T:\Eligibility Support\EA_FAD\EA_FAD_Teams\Induction Team\Case Review Documents\04.2016 Hire Group\Case Review Logs\"
        myRealWkbkName = WorkbookPath & "List.xlsx"
       
        ' Open List.xlsx and retrieve reference workbook names and passwords
        Workbooks.Open myRealWkbkName, UpdateLinks:=False, Password:="Mark"
        Set PWWorkbook = ActiveWorkbook
        
            ' Populate wkbName() and wkbPass() arrays from List.xlsx
        ReDim myFileNames(1 To LastRowA1) As String
        ReDim myPasswords(1 To LastRowA1) As String
        For i = 1 To LastRowA1
            myFileNames(i) = PWWorkbook.Sheets(1).Cells(i, 1).Value
            myPasswords(i) = PWWorkbook.Sheets(1).Cells(i, 2).Value
        Next i
        PWWorkbook.Close False
            
        ' Open Password Protected Workbooks and close workbook
        For i = 1 To LastRowA1
            Workbooks.Open WorkbookPath & myFileNames(i), UpdateLinks:=False, Password:=myPasswords(i)
          
            Workbooks(myFileNames(i)).Close SaveChanges:=False
            Counter = Counter + 1
            pctDone = Counter / Hires   ' Progress Bar
            Call Updateprogress(pctDone)    ' Progress Bar
        Next i
    Thanks for any help or ideas!
    Last edited by armlegx; 08-04-2016 at 04:59 PM.

  2. #2
    Registered User
    Join Date
    04-21-2016
    Location
    Minneapolis, Minnesota
    MS-Off Ver
    2016
    Posts
    26

    Re: I need a macro to open a file, but I don't want to hard code the path.

    I think I have this solved. I was trying to be too complicated.

    The solution I have is this:
    Sub Getpath()
    
    
    Dim MsgPath As String
    Dim Listpath As String
    Dim Boxtitle As Variant
    
    MsgPath = "What is the path to the hire group's Review Log folder?"
    MsgPath = MsgPath & vbNewLine & vbNewLine
    MsgPath = MsgPath & "Click the Folder icon in the address bar of the folder."
    MsgPath = MsgPath & vbNewLine & "It should look like: " & vbNewLine
    MsgPath = MsgPath & "Path to April 2016 folder as example" & vbNewLine & vbNewLine
    MsgPath = MsgPath & "Copy and Paste the path into the box"
    
    Boxtitle = "Path to Case Review Folder"
    
    Listpath = Application.InputBox(MsgPath, Boxtitle)
    
    MsgBox ("The path is: " & Listpath)
    
    Range("C5").Select
    Range("C5").Value = Listpath
    
    End Sub
    And this code can go into the Macro that creates the log and summary sheet so that the path location is located in the workbook the macro I was trying to update is located in, so when it comes time to run that macro, it does not need outside input.

+ 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. VB code to open a file from a path which contains certain texts
    By shreeja178 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-15-2016, 08:03 AM
  2. vba code to open xlsm file from Network Path
    By prabhuduraraj09 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-06-2014, 04:18 AM
  3. [SOLVED] How to get a macro to open file using activesheet path
    By mbroxholme in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-16-2013, 10:31 AM
  4. consolidating excel files into one file not using hard code of folder path
    By rrakkki in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-21-2011, 09:05 AM
  5. Hard code path to refresh data from a csv file
    By R.Baxter in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-03-2009, 09:33 AM
  6. Open .CSV using macro with changing file path
    By jrhertel in forum Excel General
    Replies: 2
    Last Post: 03-13-2007, 11:23 AM
  7. Hard Code Folder Path for Browse Box
    By adamkozyra in forum Excel General
    Replies: 1
    Last Post: 02-01-2005, 10:41 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