+ Reply to Thread
Results 1 to 3 of 3

How to specify file name in a formula used in VBA when looping through multiple files?

Hybrid View

  1. #1
    Registered User
    Join Date
    08-29-2012
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    23

    How to specify file name in a formula used in VBA when looping through multiple files?

    I'm currently looping through multiple files using the following code:

        Dim wb As Workbook, sFile As String, sPath As String
        Dim itm As Variant
        Dim strFileNames  As String
         
        sPath = "C:\.......\Annual and Quarterly Budget Data\"
         
        ''Retrieve the current files in directory
        sFile = Dir(sPath)
        Do While sFile <> ""
            strFileNames = strFileNames & "," & sFile
            sFile = Dir()
        Loop
         
        ''Open each file found
        For Each itm In Split(strFileNames, ",")
            If itm <> "" Then
                Set wb = Workbooks.Open(sPath & itm)
                '' DO LOTS OF CALCULATIONS HERE
            End If
        Next itm
    Inside the loop, I have the following code, which does a SUMIF formula in one of my columns:


    BD.Sheets("Sheet1").Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Formula = "=SUMIF( 'P:\Actuary\Cash Flow Forecast\Annual and Quarterly Budget Data\[ECMQA 2012Q1.xls]Sheet1'!$D$13:$D$" & LastRow & ",D" & BD.Sheets("Sheet1").Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row & ",OFFSET('P:\Actuary\Cash Flow Forecast\Annual and Quarterly Budget Data\[ECMQA 2012Q1.xls]Sheet1'!$D$13:$D$" & LastRow & ",0,MATCH(E" & BD.Sheets("Sheet1").Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row & ",'P:\Actuary\Cash Flow Forecast\Annual and Quarterly Budget Data\[ECMQA 2012Q1.xls]Sheet1'!$D$12:$R$12,0)-1))"
    Since I am looping through all of the files, how would I change the part of the code with the file name (code below) to pick each new file and not a fixed one like I have above?
    LastRow is 234 so the file and cell reference is:

    'P:\Actuary\........\[ECMQA 2012Q1.xls]Sheet1'$D$13:$D$234
    Each file has the same exact format.


    PLEASE HELP!
    Thank you!

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: How to specify file name in a formula used in VBA when looping through multiple files?

    Untested, but try:
        BD.Sheets("Sheet1").Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Formula = _
            "=SUMIF( '" & sPath & "[" & itm & "]Sheet1'!$D$13:$D$" & LastRow & _
            ",D" & BD.Sheets("Sheet1").Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row & _
            ",OFFSET('" & sPath & "[" & itm & "]Sheet1'!$D$13:$D$" & LastRow & _
            ",0,MATCH(E" & BD.Sheets("Sheet1").Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row & _
            ",'" & sPath & "[" & itm & "]Sheet1'!$D$12:$R$12,0)-1))"
    I'm not sure what the BD variable is, when you use wb in the For Next loop. Also, each workbook could be opened in the While Loop and get rid of the For Next loop altogether.
    Post responsibly. Search for excelforum.com

  3. #3
    Registered User
    Join Date
    08-29-2012
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: How to specify file name in a formula used in VBA when looping through multiple files?

    Thanks I'll test it out when I get back to work on Monday!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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