Macro to loop based on cell value to sum varying size of rows

esSant593

New Member
Joined
Nov 11, 2002
Messages
11
I need help to develop a looping macro formula to sum range of varying size of rows based on cell value ‘Total’ in column B. The sample data are downloaded with amount on Totals but not formula. The help I need is to develop formula on each row with 'Total' in column C then copy them to last column of the same row, however, need to loop on each row with ‘Total’. If you need sample file let me know.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
esSant593,

Please post a screenshot of your sheet(s), what you have and what you expect to achieve, with Excel Jeanie HTML 4 (contains graphic instructions).
http://www.excel-jeanie-html.de/html/hlp_schnell_en.php

Or, you can upload your workbook to www.box.net and provide us with a link to your workbook.

See spreadsheet. The spreadsheets were downloaded and no formula on the Total row. The formula(Example: =sum(C38:c41) on C42 will be adding all amount in that column of the particular group. The macro will loop to all rows with Total in col. B. Once the formula has been set up on that row it will be copied to columns D-J.

Excel Workbook
ABCDEFGHIJ
1Company: xyzAFTER
2ABCDEFGHIJ
30080055539RA(15.50)0.000.000.000.000.000.00(15.50)
4Total(15.50)0.000.000.000.000.000.00(15.50)
5
60080123887PY(38,088.00)0.000.00(38,088.00)0.000.000.000.00
7Total(38,088.00)0.000.00(38,088.00)0.000.000.000.00
8
90080072063IN56,448.000.0056,448.000.000.000.000.000.00
10Total56,448.000.0056,448.000.000.000.000.000.00
11
120080055636AD(277.00)0.000.000.00(15.00)0.000.00(262.00)
130080055636IN4,032.004,032.000.000.000.000.000.000.00
140080055636PP(41.50)2,340.000.00(2,340.00)0.00(41.50)0.000.00
15Total3,713.506,372.000.00(2,340.00)(15.00)(41.50)0.00(262.00)
16
170080055599AD7,264.000.000.000.007,264.000.000.000.00
180080055599IN447,433.50320,047.2085,038.6042,347.700.000.000.000.00
190080055599PP54,422.000.000.00(13,140.00)67,562.000.000.000.00
200080055599RA(267.50)0.000.000.00(252.00)0.00(15.50)0.00
210080055599SH1,976.500.000.000.00(399.50)0.002,376.000.00
22Total510,828.50320,047.2085,038.6029,207.7074,174.500.002,360.500.00
23
240080055688AD56,986.070.000.0020,545.4136,440.660.000.000.00
250080055688IN4,558,844.793,360,248.59744,555.90454,040.300.000.000.000.00
260080055688PC355.000.000.000.000.000.000.00355.00
270080055688PD(72.00)0.000.000.00(72.00)0.000.000.00
280080055688PP76.500.000.0076.500.000.000.000.00
290080055688RA(11,484.18)0.000.00(1,461.22)10,076.940.00(240.00)(19,859.90)
30Total4,604,706.183,360,248.59744,555.90473,200.9946,445.600.00(240.00)(19,504.90)
31
320080055822RA(258.50)0.000.000.000.00(252.00)(6.50)0.00
33Total(258.50)0.000.000.000.00(252.00)(6.50)0.00
34
350080055603IN500,889.60500,889.600.000.000.000.000.000.00
36Total500,889.60500,889.600.000.000.000.000.000.00
37
380080110039IN18,144.000.000.0018,144.000.000.000.000.00
390080110039PD9,216.000.000.000.000.000.009,216.000.00
400080110039PP(11,668.50)0.000.000.000.00(2,073.50)0.00(9,595.00)
410080110039PY(17,765.00)0.000.000.000.000.00(16,014.00)(1,751.00)
42Total(2,073.50)0.000.0018,144.000.00(2,073.50)(6,798.00)(11,346.00)
43
44GrandTotal36,859,838.4523,974,387.9910,984,394.531,584,858.74150,130.9788,672.93124,648.61(47,255.32)
45
46Looping macro to set up each ROW with Total with a formula. Once the formula has been set up it will be copied across to column D - J.
DATA_US
 
Upvote 0
esSant593,


Sample data before the macro:


Excel Workbook
ABCDEFGHIJ
1Company: xyzAFTER
2ABCDEFGHIJ
380055539RA-15.5000000-15.5
4Total-15.5000000-15.5
5
680123887PY-38,088.0000-38,088.000000
7Total-38,088.0000-38,088.000000
8
980072063IN56,448.00056,448.0000000
10Total56,448.00056,448.0000000
11
1280055636AD-277000-1500-262
1380055636IN4,032.004,032.00000000
1480055636PP-41.52,340.000-2,340.000-41.500
15Total3,713.506,372.000-2,340.00-15-41.50-262
16
1780055599AD7,264.000007,264.00000
1880055599IN447,433.50320,047.2085,038.6042,347.700000
1980055599PP54,422.0000-13,140.0067,562.00000
2080055599RA-267.5000-2520-15.50
2180055599SH1,976.50000-399.502,376.000
22Total510,828.50320,047.2085,038.6029,207.7074,174.5002,360.500
23
2480055688AD56,986.070020,545.4136,440.66000
2580055688IN4,558,844.793,360,248.59744,555.90454,040.300000
2680055688PC355000000355
2780055688PD-72000-72000
2880055688PP76.50076.50000
2980055688RA-11,484.1800-1,461.2210,076.940-240-19,859.90
30Total4,604,706.183,360,248.59744,555.90473,200.9946,445.600-240-19,504.90
31
3280055822RA-258.50000-252-6.50
33Total-258.50000-252-6.50
34
3580055603IN500,889.60500,889.60000000
36Total500,889.60500,889.60000000
37
3880110039IN18,144.000018,144.000000
3980110039PD9,216.00000009,216.000
4080110039PP-11,668.500000-2,073.500-9,595.00
4180110039PY-17,765.0000000-16,014.00-1,751.00
42Total-2,073.500018,144.000-2,073.50-6,798.00-11,346.00
43
44GrandTotal36,859,838.4523,974,387.9910,984,394.531,584,858.74150,130.9788,672.93124,648.61-47,255.32
DATA_US





After the macro:


Excel Workbook
ABCDEFGHIJ
1Company: xyzAFTER
2ABCDEFGHIJ
380055539RA-15.5000000-15.5
4Total-15.5000000-15.5
5
680123887PY-38,088.0000-38,088.000000
7Total-38,088.000.000.00-38,088.000.000.000.000.00
8
980072063IN56,448.00056,448.0000000
10Total56,448.000.0056,448.000.000.000.000.000.00
11
1280055636AD-277000-1500-262
1380055636IN4,032.004,032.00000000
1480055636PP-41.52,340.000-2,340.000-41.500
15Total3,713.506,372.000.00-2,340.00-15.00-41.500.00-262.00
16
1780055599AD7,264.000007,264.00000
1880055599IN447,433.50320,047.2085,038.6042,347.700000
1980055599PP54,422.0000-13,140.0067,562.00000
2080055599RA-267.5000-2520-15.50
2180055599SH1,976.50000-399.502,376.000
22Total510,828.50320,047.2085,038.6029,207.7074,174.500.002,360.500.00
23
2480055688AD56,986.070020,545.4136,440.66000
2580055688IN4,558,844.793,360,248.59744,555.90454,040.300000
2680055688PC355000000355
2780055688PD-72000-72000
2880055688PP76.50076.50000
2980055688RA-11,484.1800-1,461.2210,076.940-240-19,859.90
30Total4,604,706.183,360,248.59744,555.90473,200.9946,445.600.00-240.00-19,504.90
31
3280055822RA-258.50000-252-6.50
33Total-258.50000-252-6.50
34
3580055603IN500,889.60500,889.60000000
36Total500,889.60500,889.600.000.000.000.000.000.00
37
3880110039IN18,144.000018,144.000000
3980110039PD9,216.00000009,216.000
4080110039PP-11,668.500000-2,073.500-9,595.00
4180110039PY-17,765.0000000-16,014.00-1,751.00
42Total-2,073.500.000.0018,144.000.00-2,073.50-6,798.00-11,346.00
43
44GrandTotal5,636,150.284,187,557.39886,042.50480,124.69120,605.10-2,367.00-4,684.00-31,128.40
45
46
47GrandTotal36,859,838.4523,974,387.9910,984,394.531,584,858.74150,130.9788,672.93124,648.61-47,255.32
DATA_US





You will notice that your original Grand Totals on row 44 on the first screenshot, does not match my macro total. ????? Doing a manual calculation on your data, my new totals are correct.




Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Adding the Macro
1. Copy the below macro, by highlighting the macro code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub CreateTotals()
' hiker95, 06/25/2010, http://www.mrexcel.com/forum/showthread.php?t=476824
Dim SR As Long, ER As Long, LR As Long
Dim c As Range, firstaddress As String, GTotC As String
Application.ScreenUpdating = False
GTotC = "=Sum("
With Columns(2)
  Set c = .Find("Total", LookIn:=xlValues, LookAt:=xlWhole)
  If Not c Is Nothing Then
    firstaddress = c.Address
    Do
      SR = Application.Match(c.Offset(-1, -1), Columns(1), 0)
      ER = c.Row - 1
      c.Offset(, 1).Formula = "=SUM(C" & SR & ":C" & ER & ")"
      c.Offset(, 1).AutoFill Destination:=Range("C" & c.Row & ":J" & c.Row)
      GTotC = GTotC & "C" & c.Row & ","
      Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address <> firstaddress
  End If
End With
LR = Cells(Rows.Count, 1).End(xlUp).Row
If Right(GTotC, 1) = "," Then
  GTotC = Left(GTotC, Len(GTotC) - 1) & ")"
End If
Range("C" & LR).Formula = GTotC
Range("C" & LR).AutoFill Destination:=Range("C" & LR & ":J" & LR)
Application.ScreenUpdating = True
End Sub


Then run the "CreateTotals" macro.
 
Upvote 0
esSant593,


Sample data before the macro:


Excel Workbook
ABCDEFGHIJ
1Company: xyzAFTER
2ABCDEFGHIJ
380055539RA-15.5000000-15.5
4Total-15.5000000-15.5
5
680123887PY-38,088.0000-38,088.000000
7Total-38,088.0000-38,088.000000
8
980072063IN56,448.00056,448.0000000
10Total56,448.00056,448.0000000
11
1280055636AD-277000-1500-262
1380055636IN4,032.004,032.00000000
1480055636PP-41.52,340.000-2,340.000-41.500
15Total3,713.506,372.000-2,340.00-15-41.50-262
16
1780055599AD7,264.000007,264.00000
1880055599IN447,433.50320,047.2085,038.6042,347.700000
1980055599PP54,422.0000-13,140.0067,562.00000
2080055599RA-267.5000-2520-15.50
2180055599SH1,976.50000-399.502,376.000
22Total510,828.50320,047.2085,038.6029,207.7074,174.5002,360.500
23
2480055688AD56,986.070020,545.4136,440.66000
2580055688IN4,558,844.793,360,248.59744,555.90454,040.300000
2680055688PC355000000355
2780055688PD-72000-72000
2880055688PP76.50076.50000
2980055688RA-11,484.1800-1,461.2210,076.940-240-19,859.90
30Total4,604,706.183,360,248.59744,555.90473,200.9946,445.600-240-19,504.90
31
3280055822RA-258.50000-252-6.50
33Total-258.50000-252-6.50
34
3580055603IN500,889.60500,889.60000000
36Total500,889.60500,889.60000000
37
3880110039IN18,144.000018,144.000000
3980110039PD9,216.00000009,216.000
4080110039PP-11,668.500000-2,073.500-9,595.00
4180110039PY-17,765.0000000-16,014.00-1,751.00
42Total-2,073.500018,144.000-2,073.50-6,798.00-11,346.00
43
44GrandTotal36,859,838.4523,974,387.9910,984,394.531,584,858.74150,130.9788,672.93124,648.61-47,255.32
DATA_US





After the macro:


Excel Workbook
ABCDEFGHIJ
1Company: xyzAFTER
2ABCDEFGHIJ
380055539RA-15.5000000-15.5
4Total-15.5000000-15.5
5
680123887PY-38,088.0000-38,088.000000
7Total-38,088.000.000.00-38,088.000.000.000.000.00
8
980072063IN56,448.00056,448.0000000
10Total56,448.000.0056,448.000.000.000.000.000.00
11
1280055636AD-277000-1500-262
1380055636IN4,032.004,032.00000000
1480055636PP-41.52,340.000-2,340.000-41.500
15Total3,713.506,372.000.00-2,340.00-15.00-41.500.00-262.00
16
1780055599AD7,264.000007,264.00000
1880055599IN447,433.50320,047.2085,038.6042,347.700000
1980055599PP54,422.0000-13,140.0067,562.00000
2080055599RA-267.5000-2520-15.50
2180055599SH1,976.50000-399.502,376.000
22Total510,828.50320,047.2085,038.6029,207.7074,174.500.002,360.500.00
23
2480055688AD56,986.070020,545.4136,440.66000
2580055688IN4,558,844.793,360,248.59744,555.90454,040.300000
2680055688PC355000000355
2780055688PD-72000-72000
2880055688PP76.50076.50000
2980055688RA-11,484.1800-1,461.2210,076.940-240-19,859.90
30Total4,604,706.183,360,248.59744,555.90473,200.9946,445.600.00-240.00-19,504.90
31
3280055822RA-258.50000-252-6.50
33Total-258.50000-252-6.50
34
3580055603IN500,889.60500,889.60000000
36Total500,889.60500,889.600.000.000.000.000.000.00
37
3880110039IN18,144.000018,144.000000
3980110039PD9,216.00000009,216.000
4080110039PP-11,668.500000-2,073.500-9,595.00
4180110039PY-17,765.0000000-16,014.00-1,751.00
42Total-2,073.500.000.0018,144.000.00-2,073.50-6,798.00-11,346.00
43
44GrandTotal5,636,150.284,187,557.39886,042.50480,124.69120,605.10-2,367.00-4,684.00-31,128.40
45
46
47GrandTotal36,859,838.4523,974,387.9910,984,394.531,584,858.74150,130.9788,672.93124,648.61-47,255.32
DATA_US





You will notice that your original Grand Totals on row 44 on the first screenshot, does not match my macro total. ????? Doing a manual calculation on your data, my new totals are correct.




Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Adding the Macro
1. Copy the below macro, by highlighting the macro code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub CreateTotals()
' hiker95, 06/25/2010, http://www.mrexcel.com/forum/showthread.php?t=476824
Dim SR As Long, ER As Long, LR As Long
Dim c As Range, firstaddress As String, GTotC As String
Application.ScreenUpdating = False
GTotC = "=Sum("
With Columns(2)
  Set c = .Find("Total", LookIn:=xlValues, LookAt:=xlWhole)
  If Not c Is Nothing Then
    firstaddress = c.Address
    Do
      SR = Application.Match(c.Offset(-1, -1), Columns(1), 0)
      ER = c.Row - 1
      c.Offset(, 1).Formula = "=SUM(C" & SR & ":C" & ER & ")"
      c.Offset(, 1).AutoFill Destination:=Range("C" & c.Row & ":J" & c.Row)
      GTotC = GTotC & "C" & c.Row & ","
      Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address <> firstaddress
  End If
End With
LR = Cells(Rows.Count, 1).End(xlUp).Row
If Right(GTotC, 1) = "," Then
  GTotC = Left(GTotC, Len(GTotC) - 1) & ")"
End If
Range("C" & LR).Formula = GTotC
Range("C" & LR).AutoFill Destination:=Range("C" & LR & ":J" & LR)
Application.ScreenUpdating = True
End Sub


Then run the "CreateTotals" macro.
Thank you very much hiker95.
I tested the macro on my original file and I got a debug error on line of code -

Range("C" & LR).AutoFill Destination:=Range("C" & LR & ":J" & LR)

I will try it again. I need to understand all the line codes.
 
Upvote 0
It is my stupid mistake, I forgot to delete that text note on A46. Thank you very much for your expertise that you shared to help me. It is working. My other question is - will this macro will work with array of sheets or I have to activate each sheet and run that macro?
 
Upvote 0
esSant593,

will this macro will work with array of sheets or I have to activate each sheet and run that macro?


Right now the macro will work on the active worksheet.

Do you need the macro to work on all worksheets in the workbook?

Or, do you need the macro to work with array of sheets?

Please explain in more detail.
 
Upvote 0
The workbook I am trying to set up the macro have 4 worksheets with varying number of data with Total lines, some with 200 Totals & some have 1000 Totals. What is the best way? I preferred to work with array of sheets if not much to ask.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

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