Do...Loop???

boxboy30

Board Regular
Joined
Sep 16, 2011
Messages
84
How do I loop the following formula. If A2=A1 then sum(C2+C1)...keep that going until it is false. I've written the following code but I have to keep writing an IF statement in order for it to keep comparing the A column.

Sub Macro_test()
If [a2].Text = [a1].Text Then
[c1].Value = [b2].Value + [b3].Value
If
[a3].Text = [a1].Text Then
[c1].Value = [
b3].Value + [b2].Value + [b1].Value

Else: End If
Else: End If

End Sub

how do I keep it going without manually writing it?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Will this work?

Code:
Sub macro_test()
Dim LR As Long
Dim i As Long

LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR
    If Range("A" & i + 1) = Range("A" & i) Then
        Range("C" & i) = Application.Sum(Range("B" & i + 1 & ":B" & i + 2))
    End If
    If Range("A" & i + 2) = Range("A" & i) Then
        Range("C" & i) = Application.Sum(Range("B" & i & ":B" & i + 2))
    End If
Next i
End Sub
 
Upvote 0
The problem is that sometimes there are 10 cells in column A that are equal to A1...I need the same 10 cells in column B to sum.

A1: B1: C1 should result in:
s : 5: 26
s :7
s :10
s :1
s :3
t :100: 104
t :4
y :80: 84
y :4

that's why I was thinking do/loop...any suggestions
 
Upvote 0
Try this...

Code:
Sub macro_test()
Dim TR As Long
Dim LR As Long
Dim i As Long

TR = 1
LR = Range("A" & Rows.Count).End(xlUp).Row

    For i = 2 To LR + 1
        If Range("A" & i) <> Range("A" & i - 1) Then
            Range("C" & TR) = Application.Sum(Range("B" & TR & ":B" & i - 1))
            TR = i
        End If
    Next i
End Sub
 
Upvote 0
This kind of works...it starts off really accurate, then it drifts to not being so correct...like:

A1: B1: C1:
s : 5: 26
s :7
s :10
s :1: 4 INCORRECT
s :3
t :100: 104
t :4
y :80: 84
y :4

Is it because it refering back to A1? or is it refering back to the cell above it?
 
Upvote 0
This kind of works...it starts off really accurate, then it drifts to not being so correct...like:

A1: B1: C1:
s : 5: 26
s :7
s :10
s :1: 4 INCORRECT
s :3
t :100: 104
t :4
y :80: 84
y :4

Is it because it refering back to A1? or is it refering back to the cell above it?

It's referring to the cell above in the calculation...is there an extra space in A4 and A5?
 
Upvote 0
Silly me! I had the filter on! This is awesome...so can you explain the code to me so I can understand it. I want to now add some variables to it (including other cells)...if(D1 = "Cancelled", then don't include), etc.

You're awesome by the way!
 
Upvote 0
and can i make it filter by month...so it will only for every month it starts over...month being in column E?
 
Upvote 0
Basically, the TR is the indicator of the Top Row and i loops thru to find where Column A changes...it then sum from the top row to the bottom of that group. It then Changed TR to be the new top row and repeats thru the end of the cells in A with a value.
 
Upvote 0
Your original question can be done without a loop:

Code:
Sub test()
With Range("C1:C" & Range("A" & Rows.Count).End(xlUp).Row)
    .Formula = "=IF(COUNTIF($A$1:A1,A1)=1,SUMIF(A:A,A1,B:B),"""")"
    .Value = .Value
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,820
Members
452,946
Latest member
JoseDavid

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