Vba to find sum of a group

I have a file similar to the attached file.
Part of the file is an export file I got and I am filling last 2 columns.
I need to fill last 2 columns with 0(green) if it is not a total row(A1 Total, A2 Total etc).
And, I need to fill Total rows with the sum of that group(yellow).
I would like vba code to do this as I need to do this on a regular basis and bigger sets of data.
Thanks !
Sample-file.xls
MPI_ITAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nutschCommented:
try this code

Sub SumAndFill()
Dim lastrow As Long, i As Long 'define variables
Dim startRow As Long

Application.ScreenUpdating = False 'disable ScreenUpdating to avoid screen flashes


lastrow = Cells(Rows.Count, "A").End(xlUp).Row 'get last row

For i = 2 To lastrow   'loop from last Row to one
    If Right(Trim(Cells(i, 1)), 5) = "Total" Then
        Cells(i, 5).Resize(1, 2).FormulaR1C1 = "=SUM(R" & startRow & "C:R[-1]C)"
    Else
        If Cells(i, 1) <> Cells(i - 1, 1) Then startRow = i
    End If
Next

Range("E2:F" & lastrow).SpecialCells(xlCellTypeBlanks) = 0

Application.ScreenUpdating = True 'reenable ScreenUpdating
End Sub

Open in new window


Thomas

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nutschCommented:
And one small adjustment to catch errors if you don't have blank cells

Sub SumAndFill()
Dim lastrow As Long, i As Long 'define variables
Dim startRow As Long

Application.ScreenUpdating = False 'disable ScreenUpdating to avoid screen flashes


lastrow = Cells(Rows.Count, "A").End(xlUp).Row 'get last row

For i = 2 To lastrow   'loop from last Row to one
    If Right(Trim(Cells(i, 1)), 5) = "Total" Then
        Cells(i, 5).Resize(1, 2).FormulaR1C1 = "=SUM(R[-" & i - startRow & "]C:R[-1]C)"
    Else
        If Cells(i, 1) <> Cells(i - 1, 1) Then startRow = i
    End If
Next

On Error Resume Next
Range("E2:F" & lastrow).SpecialCells(xlCellTypeBlanks) = 0
On Error GoTo 0

Application.ScreenUpdating = True 'reenable ScreenUpdating
End Sub

Open in new window

MPI_ITAuthor Commented:
Excellent ! Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.