VBA script to capture file path

Sarah333

New Member
Joined
May 30, 2012
Messages
13
I need a script to capture the A file path, as my macro opens named files which reside in a different location to the master file that contains the macro, I want to avoid hardcoding the paths and end users having to change the VBA code every month for example

Master file resides in
c:\accounts\MasterFiles\

the files that the master file macro uses are located in: (This location changes every month.)

c:\accounts\monthendfiles\2012\Jan\



so I either need the macro to reference a range in the spreadsheet that the end user inputs the year and month folder names so this can be called into the path name, or a prompt and browse to the folder path that can be referenced in the macro.

woudl really appreciate any assistance
Sarah
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Code:
Sub SelectFolder()
    Dim fd As FileDialog
    Dim sPath As String
    
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    
    If fd.Show = -1 Then
        sPath = fd.SelectedItems(1)
    End If
    
    'sPath now holds the path to the folder or nothing if the user clicked the cancel button
    MsgBox sPath
End Sub
 
Upvote 0
Assuming you have Year in column A and month in column B. Put this code in your master file. All the data should be filled to the last row.
Code:
Sub a()
    Dim LR_A As Long, i As Long
    LR_A = Range("A" & Rows.count).End(xlUp).Row

    Dim masterFilePath As String, filePath As String
    'You can return the path of master file with this.
    masterFilePath = ThisWorkbook.Path

    'You can append the strings with '&'
    For i = 2 To LR_A
        filePath = ThisWorkbook.Path & "\" & Range("A" & i).Value & "\" Range("B" & i).Value
        'Do whatever in here.
    Next i 
End Sub

Not tested.
 
Upvote 0
vaskov17

this works well thank you but when I try to use the sPath variable to open a workbook I get an error

Workbooks.Open Filename:=sPath & "load1.xls"

what am I doing wrong?
Thanks
Sarah
 
Upvote 0
kpark91
Thank you very much this works :)
I had tried every combination bar that!

thanks & have a good day
Sarah
 
Upvote 0
Following on from the above, the prompt for file name worked well but I have a similar file that really needs this to be non prompt, therefore I have written the below but its not working :(

v_A: is the path stored in the spreadsheet to the lowest comon parent ie "C:\Accounts\Monthend\"

v_B: is the month folder name that changes every mnoth ie "05 May" this cell is a text format

can you help please?
thanks in advance
-----------------------------------------

Sub TestOpen()
Dim v_A As Object
Dim v_B As Object
Dim v_C As String
Dim MyFilePath As String


v_A = Worksheets("Sheet1").Range("C1")
v_B = Worksheets("Sheet1").Range("C2")
v_C = "\02 - Emails\Files sent"
MyFilePath = v_A & "\" & v_B & v_C


Workbooks.Open Filename:=MyFilePath & "\Test1.xls"

End Sub

note using xp and excel 2003(waiting on windows 7 !)
 
Last edited:
Upvote 0
So Close!
Rich (BB code):
Sub TestOpen()
 Dim v_A As String
 Dim v_B As String
 Dim v_C As String
 Dim MyFilePath As String
  
 
 v_A = Worksheets("Sheet1").Range("C1").Value
 v_B = Worksheets("Sheet1").Range("C2").Value
 v_C = "\02 - Emails\Files sent"
 MyFilePath = v_A & "\" & v_B & v_C
 
 
      Workbooks.Open Filename:=MyFilePath & "\Test1.xls"
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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