VBA to Total column D based on name column A

rspalding

Active Member
Joined
Sep 4, 2009
Messages
295
Office Version
  1. 365
Platform
  1. Windows
Hi and thanks for the help.​

I have a list of names in column A and numbers in column D. I want to total up the numbers in column D after the last entry in column A or D and add the word total next to it in column C. See below please

AlvarezRamiro07/26/20248.000
Total
8.00
BalzanMariana07/26/20248.000
Total
8.00
CardozaRey07/25/202413.250
CardozaRey07/26/202413.500
CardozaRey07/27/202413.250
Total
40.00
ChavezJean07/24/20248.000
ChavezJean07/25/20248.000
ChavezJean07/26/20248.000
DominguezJulio07/25/20248.000
DominguezJulio07/26/20248.000
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Please try this:

VBA Code:
Sub AddTotals()
  Dim Cel As Range
  Dim Rng As Range
  Dim FirstRow As Long
  Dim LastRow As Long
  Dim X As Long
  
  Set Rng = Range(Range("D1"), Range("D" & Cells.Rows.Count).End(xlUp))
  FirstRow = Rng.Resize(1, 1).Row
  LastRow = Rng.Resize(1, 1).Offset(Rng.Rows.Count, 0).Row
  
  For X = LastRow To FirstRow + 1 Step -1
    If Cells(X, 1) & Cells(X, 2) <> Cells(X - 1, 1) & Cells(X - 1, 2) Then
      Cells(X, 1).EntireRow.Insert
      Cells(X, 1).Value = "Total"
      Cells(X, 1).HorizontalAlignment = xlRight
      Cells(X, 2).Value = Application.SumIfs(Range("D:D"), Range("A:A"), Cells(X - 1, 1), Range("B:B"), Cells(X - 1, 2))
      Cells(X, 2).NumberFormat = "#,##0.0"
      Range(Cells(X, 1), Cells(X, 2)).Font.Bold = True
    End If
  Next X
  
End Sub
 
Upvote 0
Jeff,

Thank you for your help. I did make a mistake in my info. The total should be in row "F" and the word "TOTAL in row "E".
 
Upvote 0
New Code to move the totals

Sub AddTotals()
Dim Cel As Range
Dim Rng As Range
Dim FirstRow As Long
Dim LastRow As Long
Dim X As Long

Set Rng = Range(Range("D1"), Range("D" & Cells.Rows.Count).End(xlUp))
FirstRow = Rng.Resize(1, 1).Row
LastRow = Rng.Resize(1, 1).Offset(Rng.Rows.Count, 0).Row

For X = LastRow To FirstRow + 1 Step -1
If Cells(X, 1) & Cells(X, 2) <> Cells(X - 1, 1) & Cells(X - 1, 2) Then
Cells(X, 1).EntireRow.Insert
Cells(X, 3).Value = "Total"
Cells(X, 3).HorizontalAlignment = xlRight
Cells(X, 4).Value = Application.SumIfs(Range("D:D"), Range("A:A"), Cells(X - 1, 1), Range("B:B"), Cells(X - 1, 2))
Cells(X, 4).NumberFormat = "#,##0.0"
Range(Cells(X, 3), Cells(X, 4)).Font.Bold = True
End If
Next X

End Sub
 
Upvote 0
Jeff,
Still not quite there. I made some changes but not getting a sum and deleing the number on the first line of name. The total should only be below.

LastNameFirstNameEffectiveDateHours
AlvarezRamiro07/26/20248.000
Total
0.0​
CardozaRey07/25/2024Total
0.0​
CardozaRey07/26/202413.500
CardozaRey07/27/202413.250
Total
0.0​
ChavezJean07/24/2024Total
0.0​
ChavezJean07/25/20248.000
ChavezJean07/26/20248.000
Total
0.0​
 
Upvote 0
So I assumed you were starting with a blank set of data with no subtotals. If that is not the case, then we'll have to adjust. The macro worked on the set of data as below.

Book1 20240813.xlsm
ABCD
1AlvarezRamiro7/26/20248
2BalzanMariana7/26/20248
3CardozaRey7/25/202413.25
4CardozaRey7/26/202413.5
5CardozaRey7/27/202413.25
6ChavezJean7/24/20248
7ChavezJean7/25/20248
8ChavezJean7/26/20248
9DominguezJulio7/25/20248
10DominguezJulio7/26/20248
Sheet3
 
Upvote 0
Looks like this only with more names.

LastNameFirstNameEffectiveDateHours
AlvarezRamiro07/26/20248.000
CardozaRey07/25/202413.250
CardozaRey07/26/202413.500
CardozaRey07/27/202413.250
ChavezJean07/24/20248.000
ChavezJean07/25/20248.000
ChavezJean07/26/20248.000
DominguezJulio07/25/20248.000
DominguezJulio07/26/20248.000
 
Upvote 0
Jeff,
I made my sheet look like yours and it works except that the hours have some type of a link and won't total. If I retype the hours so the link is gone all works. Can you give me a code to delete the link please.

1723641608910.png

Thanks,

Robert
 
Upvote 0
Can you show me what is in the cell. It looks like the cells are text maybe instead of numbers. Do you have XL2BB so you can post a visual of the result?
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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