vba to sum up each column for each employee on column (A)

Medhat

New Member
Joined
Aug 11, 2015
Messages
5
First I am trying at sum up each column for each employee on column (A). starting from column E till last column.
the issue I have that the code below refering to original data but ignoring the rest of columns at bottom as the code adds new row which contains total
you will notice (last 5 rows have been ignored as below code added 5 new total rows to original data)
Looking for responds.

VBA Code:
Sub SumColumnsForEachName()
    Dim ws As Worksheet
    Dim lastRow As Long, lastCol As Long
    Dim i As Long, startRow As Long
    Dim col As Long
    Dim sumRange As Range
    Dim isEmpty As Boolean
   
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name
   
    ' Find the last row with data in column A (or any column in use)
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    ' Find the last column with data in row 1
    lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
   
    startRow = 2 ' Assuming the first row has headers
   
    For i = 2 To lastRow + 1 ' Loop through each row, including the last active row
        ' Check if the name changes or it's the last row
        If ws.Cells(i, 1).Value <> ws.Cells(i - 1, 1).Value Or i = lastRow + 1 Then
            If i > startRow Then
                ' Apply light gray background to existing data (excluding total row)
                ws.Range(ws.Cells(startRow, 1), ws.Cells(i - 1, lastCol)).Interior.Color = RGB(211, 211, 211)
               
                ' Insert a new row to place the totals
                ws.Rows(i).Insert Shift:=xlDown
               
                ' Sum each column for the range from startRow to i-1, only if not empty
                For col = 5 To lastCol
                    ' Define the range to sum
                    Set sumRange = ws.Range(ws.Cells(startRow, col), ws.Cells(i - 1, col))
                   
                    ' Check if the range is not completely empty
                    isEmpty = Application.WorksheetFunction.CountA(sumRange) = 0
                   
                    If Not isEmpty Then
                        ' Apply the sum formula if the column is not empty
                        ws.Cells(i, col).Formula = "=SUM(" & sumRange.Address & ")"
                    End If
                Next col
               
                ' Apply formatting to the totals row
                With ws.Cells(i, 2).Resize(1, lastCol - 1)
                    .Interior.Color = RGB(0, 0, 0) ' Black background
                    .Font.Color = RGB(255, 255, 255) ' White text
                    .Font.Bold = True
                End With
               
                ' Update startRow for the next employee group
                startRow = i + 1
            End If
        End If
    Next i
End Sub
 
Last edited by a moderator:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Typically if you are inserting or deleting rows you want to work from the bottom up.
In your case inserting a row means that the lastRow is pushing down the sheet and is what is causing your loop to fall short of number of rows that are actually there.

Here is your code modified to work from the bottom up.
VBA Code:
Sub SumColumnsForEachName()
    Dim ws As Worksheet
    Dim lastRow As Long, lastCol As Long
    Dim i As Long, startRow As Long, lastRowSect As Long
    Dim col As Long
    Dim sumRange As Range
    Dim isEmpty As Boolean
    
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name
    
    ' Find the last row with data in column A (or any column in use)
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    ' Find the last column with data in row 1
    lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
    
    startRow = 2 ' Assuming the first row has headers
    lastRowSect = lastRow
    
    For i = lastRow To 2 Step -1        ' Loop through each row
        ' Check if the name changes or it's the last row
        If ws.Cells(i, 1).Value <> ws.Cells(i - 1, 1).Value Then
            ' Apply light gray background to existing data (excluding total row)
                ws.Range(ws.Cells(i, 1), ws.Cells(lastRowSect, lastCol)).Interior.Color = RGB(211, 211, 211)
                
                ' Insert a new row to place the totals
                ws.Rows(lastRowSect + 1).Insert Shift:=xlDown
                
                ' Sum each column for the range from startRow to i-1, only if not empty
                For col = 5 To lastCol
                    ' Define the range to sum
                    Set sumRange = ws.Range(ws.Cells(i, col), ws.Cells(lastRowSect, col))
                    
                    ' Check if the range is not completely empty
                    isEmpty = Application.WorksheetFunction.CountA(sumRange) = 0
                    
                    If Not isEmpty Then
                    ' Apply the sum formula if the column is not empty
                    ws.Cells(lastRowSect + 1, col).Formula = "=SUM(" & sumRange.Address & ")"
                    End If
                Next col
                
                ' Apply formatting to the totals row
                With ws.Cells(lastRowSect + 1, 2).Resize(1, lastCol - 1)
                    .Interior.Color = RGB(0, 0, 0) ' Black background
                    .Font.Color = RGB(255, 255, 255) ' White text
                    .Font.Bold = True
                End With
                
                ' Update startRow for the next employee group
                lastRowSect = i - 1
        End If
    Next i
End Sub
 
Upvote 0
Solution
Try. Change this line
VBA Code:
  For i = 2 To lastRow + 1 ' Loop through each row, including the last active row
as
i = 2
VBA Code:
Do while ws.Cells(i, 1).Value <>""   ' Loop through each row, including the last active row

This line
VBA Code:
  Next i
as
VBA Code:
Loop
 
Upvote 0
@kvsrinivasamurthy - Did you try your changes ?
Since you are not using the for/next loop the code needs to advance and keep track of "i".

Here is my test data if you want to try your code:
20240822 VBA Insert Subtotal Rows Medhat.xlsm
ABCDEFG
1NameCol2Col3Col4Amt 1Amt 2Amt 3
2Apple201201020
3Apple301301030
4Apple401401040
5Apple501501050
6Orange601601060
7Orange701701070
8Orange801801080
9Orange901901090
10Pear1002001100
11Pear1102101110
12Pear1202201120
13Kiwi1302301130
14Melon1402401140
15Melon1502501150
16Melon1602601160
17
Sheet1
Cell Formulas
RangeFormula
F2:F16F2=E2+100
G2:G16G2=1000+E2
 
Upvote 0
Try. Change this line
VBA Code:
  For i = 2 To lastRow + 1 ' Loop through each row, including the last active row
as
i = 2
VBA Code:
Do while ws.Cells(i, 1).Value <>""   ' Loop through each row, including the last active row

This line
VBA Code:
  Next i
as
VBA Code:
Loop
Thanks very much Alex!!!
Works as I wish - you are really expertise in VBA coding, Much appreciated.
 
Upvote 0
@ Alex Blakenburg
I have corrected the code.
VBA Code:
Sub SumColumnsForEachName()
    Dim ws As Worksheet
    Dim lastRow As Long, lastCol As Long
    Dim i As Long, startRow As Long
    Dim col As Long
    Dim sumRange As Range
    Dim isEmpty As Boolean
   
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name
   
    ' Find the last row with data in column A (or any column in use)
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    ' Find the last column with data in row 1
    lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
   
    startRow = 2 ' Assuming the first row has headers
   i = 3
    'For i = 2 To lastRow + 1 ' Loop through each row, including the last active row
    Do While ws.Cells(i, 1) <> ""
        
        ' Check if the name changes or it's the last row
        If ws.Cells(i, 1).Value <> ws.Cells(i - 1, 1).Value Or ws.Cells(i + 1, 1) = "" Then 'Or i = lastRow + 1
            If ws.Cells(i + 1, 1) = "" Then i = i + 1
            If i > startRow Then
                ' Apply light gray background to existing data (excluding total row)
                ws.Range(ws.Cells(startRow, 1), ws.Cells(i - 1, lastCol)).Interior.Color = RGB(211, 211, 211)
               
                ' Insert a new row to place the totals
                ws.Rows(i).Insert Shift:=xlDown
               
                ' Sum each column for the range from startRow to i-1, only if not empty
                For col = 5 To lastCol
                    ' Define the range to sum
                    Set sumRange = ws.Range(ws.Cells(startRow, col), ws.Cells(i - 1, col))
                   
                    ' Check if the range is not completely empty
                    isEmpty = Application.WorksheetFunction.CountA(sumRange) = 0
                   
                    If Not isEmpty Then
                        ' Apply the sum formula if the column is not empty
                        ws.Cells(i, col).Formula = "=SUM(" & sumRange.Address & ")"
                    End If
                Next col
               
                ' Apply formatting to the totals row
                With ws.Cells(i, 2).Resize(1, lastCol - 1)
                    .Interior.Color = RGB(0, 0, 0) ' Black background
                    .Font.Color = RGB(255, 255, 255) ' White text
                    .Font.Bold = True
                End With
               
                ' Update startRow for the next employee group
                startRow = i + 1
            End If
        End If
     i = i + 1
     Loop
    'Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,916
Messages
6,175,361
Members
452,638
Latest member
Oluwabukunmi

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