Assistance with Sum Loop

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,486
I have set Rng3 = Visible Cells between A81 to A206 - I'm trying to loop through Rng3 and produce the Subtotals of the 3 employees (amts furnished) in Column E.
Looping drives me crazy...
Can someone provide the code to calculate and post the subtotals I need,,, PLEASE!!
TIA,
Jim
Excel Workbook
ABCDEF
1FullNameTotal Paid HoursDepartmentSalaried?Sub-Total Salaried
81Bryant, John3.42MarketingYes
82Bryant, John2.72MarketingYes
83Bryant, John5.37MarketingYes
84Bryant, John2.18MarketingYes
85Bryant, John5.57MarketingYes
86Bryant, John9.53MarketingYes
87Bryant, John8.00MarketingYes36.79<<<
168Gambee, Brandon7.68DeliYes
169Gambee, Brandon8.38DeliYes
170Gambee, Brandon7.48DeliYes
171Gambee, Brandon8.08DeliYes
172Gambee, Brandon8.45DeliYes
173Gambee, Brandon8.70DeliYes48.77<<<
201Greenman, Carleen8.23OfficeYes
202Greenman, Carleen8.57OfficeYes
203Greenman, Carleen8.00OfficeYes
204Greenman, Carleen8.17OfficeYes
205Greenman, Carleen7.95OfficeYes
206Greenman, Carleen8.18OfficeYes49.10<<<
Sheet1
Excel 2007
Cell Formulas
RangeFormula
D81=IF(ISERROR(VLOOKUP(A81,SalEmploys,1,FALSE)),"","Yes")
Excel Workbook
NameRefers To
SalEmploys=LookUps!$I$4:$I$10
Workbook Defined Names
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hello Jim. This should do it, but untested so may need some tweaking:
Code:
Sub LoopAndSum()
Dim Rng3 As Range, sTot As Double
Set Rng3 = Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible)
rws = Rng3.Rows.Count
Set Rng3 = Rng3.Offset(1, 0).Resize(rws - 1)
sTot = 0
With Rng3
    For i = 1 To .Rows.Count
        If .Cells(i, 1).Value = .Cells(i + 1, 1).Value Then
            sTot = sTot + .Cells(i, 2).Value
        Else
            .Cells(i + 1, 5).Value = sTot + .Cells(i, 2).Value
            sTot = 0
        End If
    Next i
End With
End Sub
 
Upvote 0
JoMo - Thanks -- I'm playing around with your code (Chged Rng3 to Rng1), but...
Stepping thru the code (after line 4) in the immediate window I enter

? Rng1.address and get...
$A$81:$A$87,$A$168:$A$173,$A$201:$A$206

there are a total of 19 rows (7+6+6)

But the code rws = Rng3.Rows.Count
is producing 7

hummmmm..... What's wrong here?

Code:
Sub LoopAndSum()
Dim Rng1 As Range, sTot As Double
LR = Range("A" & Rows.Count).End(xlUp).Row
Set Rng1 = Range("A2:A" & LR).SpecialCells(xlCellTypeVisible)

Thanks,

jim
 
Upvote 0
Jim,

The Rows property of the Range object only applies to the first Area of a range. Here, because the cells aren't contiguous, there are multiple areas (each separated by a comma in the range's address).

This ought to give you the number of rows:

Code:
Intersect(Columns(1), Rng1).Cells.Count
 
Last edited:
Upvote 0
Since Excel has a built-in Subtotals feature, I would try to let it do (at least most of) the work.

I have assumed that columns A and B do not contain formulas. If that is not the case, please give some more details as I would need to re-think the code.

Suggest testing in a copy of your workbook.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Get_Totals()<br>    <SPAN style="color:#00007F">Dim</SPAN> Cel <SPAN style="color:#00007F">As</SPAN> Range<br><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Columns("A:B")<br>        .Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2), _<br>            Replace:=True, PageBreaks:=False, SummaryBelowData:=<SPAN style="color:#00007F">True</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Cel <SPAN style="color:#00007F">In</SPAN> .SpecialCells(xlCellTypeFormulas)<br>            <SPAN style="color:#00007F">If</SPAN> Cel.Offset(, -1).Value <> "Grand Total" <SPAN style="color:#00007F">Then</SPAN><br>                Cel.Offset(-1, 3).Value = Cel.Value<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> Cel<br>        .RemoveSubtotal<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
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