0

I have data that I need to create some sort of loop for. Basically, I'm searching for a column that has the info I need using a string search, then summing based on various criteria. My problem is that the data is separated by months (using a numberical designation; i.e; Jan=01, Feb=02, etc) and I need a way to add up all the info to date. The formula I am using is below:

=SUMPRODUCT(SUMIFS(INDEX('Program Data'!A:GA,0,MATCH("*"&AC7&" "&AC10&" -"&A8&" "&B15&"*",
  'Program Data'!1:1,0)),'Program Data'!$M:$M,$A2,'Program Data'!$E:$E,B2:B6))

Where A8 is the cell that contains the selected month of interest. So lets say A8=08 (August). I want to execute this formula for all previous values of A8 and add them together, adding upwards UNTIL A8 equals the current selection. (or while A8<09, however this can be accomplished)

So assuming A8=08, I want it to start at A8=01, run this code and find the value. Then run A8=02, find that value, then add it to the first value, continuing this until we get to A8=08 and then reporting the final value. Since the single-digit months are written in two-digits (ie; 01, 02, 03, etc) and searched using the leading 0, I think its fairly important the loop account for it up until we get two-digit months (10, 11, 12)....

Something along the lines of (and this is NOT code, just a general thought-process):

Sub Loop1()
Dim x as integer
x=01
Do until x = Cell A8
      (insert the formula above, but with "&A8&" = x)
x + 1 = x
End Sub

but even this thought-process doesn't seem to add the resulting formula values together, so I need help with that. I don't know how to perform do loops in excel, so any help you care to offer would be sincerely appreciated. I'm afraid I don't really know where to start, or the logical order each statement. Thanks for looking!

EDIT

Please excuse my poor description, hopefully this clarifies:

The code is a part of a table. Cell A8 is a drop-down selection where you choose the numberical month you want to examine (01, 02 --> 12). The formula then looks for a string using a set of reference cells, lets say "Rev Bud 08 -2013"(where 08 comes from your selection), then looks for a column with that title then performs the sumifs. But I want a year-to-date, so i want it to exceute the formula once for each x, starting at x=1, and up to and including whatever value is selected in cell A8, then add the results from all of the executions together and display that value in a cell of my choosing, let's say cell C50. So I want it to do the sumifs starting on the column with the title "Rev Bud 01 -2013" then "Rev Bud 02 -2013" .... all the way until "Rev Bud [my selection] -2013" and add all of those together.

Kara
  • 135
  • 1
  • 3
  • 12

1 Answers1

1

I'm not quite following the question in terms of how the formula is supposed to change and what cell the final formula is supposed to reside in but here is how to structure your loop:

Sub Loop1()
    Dim x As Integer: x = 1

    Do Until x = Range("A8") + 1
        range("'your cell'").Formula = 'your formula'

        x = x + 1
    Loop
End Sub

I'm guessing you want to loop through each month and add that part to the formula and then put the result in some cell like this:

Sub Loop1()
    Dim x As Integer: x = 1
    Dim formulaString As String

    Do Until x = Range("A8") + 1
        If formulaString = "" Then
            formulaString = "="
        Else
            formulaString = formulaString & " + "
        End If

        formulaString = formulaString + "SUMPRODUCT(SUMIFS(INDEX('Program Data'!A:GA,0,MATCH("
        formulaString = formulaString & """*""" & "&AC7&""" & Chr(32) & """&AC10&""" & " -"""
        formulaString = formulaString & "&A" & x & "&"" """
        formulaString = formulaString & "&B15&" & """*""" & ",'Program Data'!1:1,0)),"
        formulaString = formulaString & "'Program Data'!$M:$M,$A2,'Program Data'!$E:$E,B2:B6))"

        x = x + 1
    Loop

    Range("C1").Value = formulaString
End Sub
Automate This
  • 30,726
  • 11
  • 60
  • 82
  • See edit to second code segment. I'm breaking up your formula into chunks for readability purposes. In the third formulaString statement you'll see the 'x' variable. Now, each time it loops I'm adding one formula to the next with the change of x and putting everything into C1. – Automate This Oct 30 '13 at 19:47
  • When I run this I simply get " + SUMPRODUCT(SUMIFS(INDEX('Program Data'!A:GA,0,MATCH("*"&AC7&" "&AC10&" -"&A&" "&B15&"*",'Program Data'!1:1,0)),'Program Data'!$M:$M,$A2,'Program Data'!$E:$E,B2:B6))" as a text value in C1. Am I missing how to modify it or something? – Kara Oct 30 '13 at 19:58
  • 1
    Sorry, see my most recent edit, I switched i for x and got rid of the ' that made the formula text. – Automate This Oct 30 '13 at 20:03
  • 1
    Sorry for all the edits, I added one more to loop through A8 by adding + 1 to the loop until value. – Automate This Oct 30 '13 at 20:09
  • Hi Portland Runner, I was wondering if you could possibly tell me how to modify the string so it's only looking for "x", not the long, multi-celled string as above? I've tried to modify it, but I keep getting errors. I sincerely appreciate your help. – Kara Nov 14 '13 at 15:41
  • 1
    Sorry Kara, I'm not quite following the question. Looking only for x? X is a counter from 1 (Jan) upto the month selected in 'A8'. Can you explain a little more? Might be good to post a new question. – Automate This Nov 14 '13 at 16:25
  • The columns are now named "1", "2", "3", etc. So lets say I just want to search for these numbered column names. They're non-adjacent, so they still need to be searched for (cant go A:G this way) Say I want to add this up for columns named 20 - 25. If cell A8=25 (where I want the loop to stop), I'd set x=20, then itd execute for x=20, x=21, ... x=A8=25, searching for a column named 20, then named 21, etc, then adding them all together. Does that make sense? – Kara Nov 14 '13 at 16:41