How use vba SUM function in specific row for two columns

Ali M

Active Member
Joined
Oct 10, 2021
Messages
287
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hello
I want to sum numeric numbers for columns D,E and show result in TOTAL row .
the data begins B2:F so should sum numeric values from D2,E2 until reach lastrow contains TOTAL word in column B .every time should update the summing in TOTAL row for columns D,E when run the macro every time.
how can I use vba WorksheetFunction.Sum for that ,guys?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I would find the totals row and deduct a bumber to suit your sheet.
Then set the Totals cell value to that formula.

However if the Totals row is not moving, you can hard code that formula.
 
Upvote 0
Then set the Totals cell value to that formula.
I can't because the TOTAL row will change in location every time , is not fixing . that's why I want vba
 
Upvote 0
You can as you will know the row where the totals are.
 
Upvote 0
Please show us an example of your data layout and expected results.

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.
 
Upvote 0
ok
example
before
q
BCDEF
1BATCHSER.NOINPUTOUTPUTNOTE
2SDD-001S-001221
3SDD-002S-00220
4SDD-003S-003121
5SDD-004S-00411
6SDD-005S-00512
7TOTAL
sh

after
q
BCDEF
1BATCHSER.NOINPUTOUTPUTNOTE
2SDD-001S-001221
3SDD-002S-00220
4SDD-003S-003121
5SDD-004S-00411
6SDD-005S-00512
7TOTAL664
sh
Cell Formulas
RangeFormula
D7:E7D7=SUM(D2:D6)

another example

before

q
BCDEF
1BATCHSER.NOINPUTOUTPUTNOTE
2SDD-001S-001221
3SDD-002S-00220
4SDD-003S-00312
5SDD-004S-00411
6TOTAL
sh


after

q
BCDEF
1BATCHSER.NOINPUTOUTPUTNOTE
2SDD-001S-001221
3SDD-002S-00220
4SDD-003S-00312
5SDD-004S-00411
6TOTAL651
sh
Cell Formulas
RangeFormula
D6:E6D6=SUM(D2:D5)
 
Upvote 0
See if this does what you want:
VBA Code:
Sub AddTotals()

    Dim lr As Long
    
'   Find last row in column B with a value
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
'   If last row in column B is "TOTAL", add totals to columns D and E
    If Cells(lr, "B").Value = "TOTAL" Then
        Cells(lr, "D").Formula = "=SUM(D2:D" & lr - 1 & ")"
        Cells(lr, "E").Formula = "=SUM(E2:E" & lr - 1 & ")"
    End If
    
End Sub
 
Upvote 1
Solution
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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