Using VBA, how can I SUM all the values from columns until I find column "Total"? Then do the same from next cell to next "total"

Tpmola99

New Member
Joined
Dec 15, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
I need to sum the values of each row and store them in the Total columns. the range of the hours columns can vary, they can be 4, 3... 20, 80... that's why it has to be detected automatically
The amount of "Hour" columns can change, so its not the same before every "Total" column. That`s why we need a automatic loop.
I have done all possible code to loop through the first line and find the Total columns but it doesn't come out any way.
Thanks for the help,
1671116108696.png
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi and welcome to MrExcel!

Try this:
VBA Code:
Sub SumTotal()
  Dim a As Variant
  Dim i As Long, j As Long
  Dim tot As Double
  
  a = Range("C1", Cells(Range("A" & Rows.Count).End(3).Row, Cells(1, Columns.Count).End(1).Column)).Value
  
  For i = 2 To UBound(a, 1)
    For j = 1 To UBound(a, 2)
      tot = tot + a(i, j)
      If a(1, j) = "Total" Then
        a(i, j) = tot
        tot = 0
      End If
    Next
  Next
  
  Range("C1").Resize(UBound(a, 1), UBound(a, 2)).Value = a
End Sub

----------------
NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

----------------
Example:
Dante Amor
ABCDEFGHIJKLMNO
1NameSurnameHour1Hour2Hour3TotalHour1Hour2Hour3Hour4Hour5TotalHour1Hour2Total
2nm1sn13554135342
3nm2sn25524131223
4nm3sn35523252534
5nm4sn45145542215
6nm5sn54312512143
7nm6sn64431341133
8nm7sn74345441434
9nm8sn82534424333
Sheet1


Result:
Dante Amor
ABCDEFGHIJKLMNO
1NameSurnameHour1Hour2Hour3TotalHour1Hour2Hour3Hour4Hour5TotalHour1Hour2Total
2nm1sn1355134135316426
3nm2sn2552124131211235
4nm3sn3552123252517347
5nm4sn4514105542218156
6nm5sn543182512111437
7nm6sn6443111341110336
8nm7sn7434115441418347
9nm8sn8253104424317336
Sheet1
 
Upvote 0
Hi and welcome to MrExcel!

Try this:
VBA Code:
Sub SumTotal()
  Dim a As Variant
  Dim i As Long, j As Long
  Dim tot As Double
 
  a = Range("C1", Cells(Range("A" & Rows.Count).End(3).Row, Cells(1, Columns.Count).End(1).Column)).Value
 
  For i = 2 To UBound(a, 1)
    For j = 1 To UBound(a, 2)
      tot = tot + a(i, j)
      If a(1, j) = "Total" Then
        a(i, j) = tot
        tot = 0
      End If
    Next
  Next
 
  Range("C1").Resize(UBound(a, 1), UBound(a, 2)).Value = a
End Sub

----------------
NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.

----------------
Example:
Dante Amor
ABCDEFGHIJKLMNO
1NameSurnameHour1Hour2Hour3TotalHour1Hour2Hour3Hour4Hour5TotalHour1Hour2Total
2nm1sn13554135342
3nm2sn25524131223
4nm3sn35523252534
5nm4sn45145542215
6nm5sn54312512143
7nm6sn64431341133
8nm7sn74345441434
9nm8sn82534424333
Sheet1


Result:
Dante Amor
ABCDEFGHIJKLMNO
1NameSurnameHour1Hour2Hour3TotalHour1Hour2Hour3Hour4Hour5TotalHour1Hour2Total
2nm1sn1355134135316426
3nm2sn2552124131211235
4nm3sn3552123252517347
5nm4sn4514105542218156
6nm5sn543182512111437
7nm6sn6443111341110336
8nm7sn7434115441418347
9nm8sn8253104424317336
Sheet1
Thank you very much for the reply!! I had been trying for days and couldn't. Thank you for all the information.
Now I have to duplicate this sheet and clean the content of the hours (I have this) but I have to leave the automatic addition as I insert the data.
It would be the same?

Thank you!
 
Upvote 0
Welcome to the MrExcel board!

I'm wondering if you want formulas in those 'Total' cells so that when you duplicate the sheet and remove the hours content, the formulas will remain ready for new data insertion?

If so, perhaps you could try this with a copy of your workbook.

VBA Code:
Sub Total_Formulas()
  Dim rTotal As Range
  Dim x As Long, fc As Long, lr As Long
  
  lr = Range("A" & Rows.Count).End(xlUp).Row
  Set rTotal = Rows(1).Find(What:="Total")
  x = rTotal.Column
  fc = 3
  Do
    rTotal.Offset(1).Resize(lr - 1).FormulaR1C1 = "=SUM(RC" & fc & ":RC[-1])"
    fc = rTotal.Column + 1
    Set rTotal = Rows(1).Find(What:="Total", After:=rTotal)
  Loop Until rTotal.Column = x
End Sub
 
Upvote 0
Now I have to duplicate this sheet and clean the content of the hours (I have this) but I have to leave the automatic addition as I insert the data.
It would be the same?
I do not understand your question.
Just do the test, duplicate your sheet, start putting the hours, and finally run my macro again, it will calculate all the totals in a second.
 
Upvote 0
I do not understand your question.
Just do the test, duplicate your sheet, start putting the hours, and finally run my macro again, it will calculate all the totals in a second.
The addition should be automatic by putting values in the hours and not running the macro again
 
Upvote 0
Can you explain what the problem is?
Yeah! When I duplicate the sheet, I empty the cell range content and insert the code provided by you. The cells remain clean but if I insert values, the sum is not done.

Thanks for the answer, I'm new in this world.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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