VBA add a criteria to a Sum function

weefatb0b

New Member
Joined
Nov 17, 2022
Messages
28
Office Version
  1. 2013
Platform
  1. Windows
Hello, I have a macro, below which works great but I have been asked to add another criteria to it, this one does it for each blank cell in column C, what I have been asked for is to only sum column C if the value in column B is in Column I, I have tried various but cant work out how to do this, any ideas would be brilliant? Thanks in advance


VBA Code:
Sub TOTALCOLUMNC()
    Dim ws As Worksheet
    Set ws = Worksheets("Position") 'define worksheet here

    Dim FirstCell As Range
    Set FirstCell = ws.Range("C2")

    Dim VeryLastCell As Range 'get very last cell as stop criteria
    Set VeryLastCell = ws.Cells(ws.Rows.Count, "I").End(xlUp)

    Do
        Dim LastCell As Range
        If FirstCell.Offset(1) = vbNullString Then 'test if there is only one cell to sum
            Set LastCell = FirstCell
        Else
            Set LastCell = FirstCell.End(xlDown)
        End If

        With LastCell.Offset(1, 0) 'this is the cell we want to write the sum
            .Value = Application.WorksheetFunction.Sum(ws.Range(FirstCell, LastCell))
            .Interior.Color = RGB(192, 192, 192)
        End With

        Set FirstCell = LastCell.Offset(2, 0)

    Loop While FirstCell.Row < VeryLastCell.Row
 
End Sub
 
Last edited by a moderator:
You didn't answer my question:
How are we supposed to identify the rows where "Head of Service" appears in column B? Do their actual values always start with "Head of Service"?"
But judging by your example in post #17, col B is either blank or has "Head of Service", is it correct?
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
You didn't answer my question:

But judging by your example in post #17, col B is either blank or has "Head of Service", is it correct?
No, there are many different values in Column B, Column B is mostly Job Titles except for the cells I have input previously to give me a sub total row for each Head of Service, if that makes sense. Head of Service is an anonomised value, its not the value used in the report
 
Upvote 0
No, there are many different values in Column B, Column B is mostly Job Titles except for the cells I have input previously to give me a sub total row for each Head of Service, if that makes sense. Head of Service is an anonomised value, its not the value used in the report
So, how are we supposed to identify the rows where "Head of Service" appears in column B?
 
Upvote 0
So, how are we supposed to identify the rows where "Head of Service" appears in column B?
VBA Code:
 Dim lastRow As Long
   Dim i As Long
   
    lastRow = Cells(Rows.Count, "B").End(xlUp).Row
   
    For i = 2 To lastRow
    If Cells(i, "B").Value = Cells(i, "I").Value Then
            Cells(i, "C").Value = WorksheetFunction.Sum(Range(Cells(i, "C"), Cells(i, "C").End(xlUp)))
        End If
    Next i
End Sub

As per post 20, I have managed to get this to work, to a certain extent, but it does not recognize where the match value in column B changes in Column I and gives me a higher than expected value, I have worked out that it is including the Sub Total value from the Column B match Value above, but cannot work out how to exclude that row....

Position NumberPosition TitleMacro TotalActual TotalDifferenceServ. AreaHOS
Head of Service 138.42Service Area 1Head of Service 1
Head of Service 2282.52244.1038.42Service Area 2Head of Service 2
 
Upvote 0
As per post 20, I have managed to get this to work, to a certain extent, but it does not recognize where the match value in column B changes in Column I and gives me a higher than expected value, I have worked out that it is including the Sub Total value from the Column B match Value above, but cannot work out how to exclude that row....
That doesn't answer my question.
Let's say Head of Service 1 is at B30, how the code should know that it is in B30?
 
Upvote 0
That doesn't answer my question.
Let's say Head of Service 1 is at B30, how the code should know that it is in B30?
My thinking is it doesnt have to, happy to know if it does, it only needs to know if the value in Column B = Value in Column I then sum all the values in Column C above it?
 
Upvote 0
My thinking is it doesnt have to, happy to know if it does, it only needs to know if the value in Column B = Value in Column I then sum all the values in Column C above it?
Sorry, I still don't understand your explanation. Hope somebody else will step in with a solution.
 
Upvote 0
Sorry, I still don't understand your explanation. Hope somebody else will step in with a solution.
Hopefully the below helps more

Position NumberPosition TitleEstablishment CountOccupiedVacantVacant %Unit/DeptServ. AreaHOS
584001Position 10.930.000.93Unit 1Service Area 1Head of service 1
584100Position 20.000.96-0.96Unit 1Service Area 1Head of service 1
584220Position 32.100.771.33Unit 1Service Area 1Head of service 1
584222Position 40.001.00-1.00Unit 1Service Area 1Head of service 1
584223Position 50.000.05-0.05Unit 1Service Area 1Head of service 1
584320Position 61.620.621.00Unit 1Service Area 1Head of service 1
584321Position 70.001.00-1.00Unit 1Service Area 1Head of service 1
584400Position 81.001.000.00Unit 1Service Area 1Head of service 1
584420Position 94.700.004.70Unit 1Service Area 1Head of service 1
584421Position 100.000.41-0.41Unit 1Service Area 1Head of service 1
584423Position 110.002.41-2.41Unit 1Service Area 1Head of service 1
584427Position 120.000.41-0.41Unit 1Service Area 1Head of service 1
584460Position 133.270.872.40Unit 1Service Area 1Head of service 1
584461Position 140.000.87-0.87Unit 1Service Area 1Head of service 1
584463Position 150.000.87-0.87Unit 1Service Area 1Head of service 1
584464Position 160.000.87-0.87Unit 1Service Area 1Head of service 1
584800Position 171.030.001.03Unit 1Service Area 1Head of service 1
584801Position 180.000.41-0.41Unit 1Service Area 1Head of service 1
584920Position 190.620.000.62Unit 1Service Area 1Head of service 1
Unit 1Service Area 1Head of service 1
580100Position 11.041.14-0.10Unit 2Service Area 1Head of service 1
580170Position 20.080.31-0.23Unit 2Service Area 1Head of service 1
580220Position 33.001.002.00Unit 2Service Area 1Head of service 1
580300Position 40.001.00-1.00Unit 2Service Area 1Head of service 1
580307Position 50.001.00-1.00Unit 2Service Area 1Head of service 1
580420Position 66.281.294.99Unit 2Service Area 1Head of service 1
580422Position 70.000.000.00Unit 2Service Area 1Head of service 1
580423Position 80.001.81-1.81Unit 2Service Area 1Head of service 1
580424Position 90.000.51-0.51Unit 2Service Area 1Head of service 1
580460Position 101.810.870.94Unit 2Service Area 1Head of service 1
580461Position 110.000.87-0.87Unit 2Service Area 1Head of service 1
580800Position 120.330.49-0.16Unit 2Service Area 1Head of service 1
580900Position 130.770.000.77Unit 2Service Area 1Head of service 1
580920Position 140.770.380.39Unit 2Service Area 1Head of service 1
Unit 2Service Area 1Head of service 1
562010Position 10.080.000.08Unit 3Service Area 1Head of service 1
562022Position 20.690.71-0.02Unit 3Service Area 1Head of service 1
562025Position 30.790.620.17Unit 3Service Area 1Head of service 1
562040Position 40.410.000.41Unit 3Service Area 1Head of service 1
Unit 3Service Area 1Head of service 1
646100Position 10.500.500.00Unit 4Service Area 1Head of service 1
646420Position 20.520.400.12Unit 4Service Area 1Head of service 1
646480Position 30.770.250.52Unit 4Service Area 1Head of service 1
646481Position 40.000.41-0.41Unit 4Service Area 1Head of service 1
Unit 4Service Area 1Head of service 1
610001Position 10.400.400.00Unit 5Service Area 1Head of service 1
610100Position 21.001.000.00Unit 5Service Area 1Head of service 1
610420Position 33.893.110.78Unit 5Service Area 1Head of service 1
610702Position 40.020.000.02Unit 5Service Area 1Head of service 1
Unit 5Service Area 1Head of service 1
Head of service 1If Value in Column B is in Column I then sum all values in Column C aboveService Area 1Head of service 1
532001Position 10.500.000.50Init 1Service Area 2Head of service 2
532210Position 21.000.000.00Init 1Service Area 2Head of service 2
532340Position 34.783.411.37Init 1Service Area 2Head of service 2
532420Position 444.0733.6110.46Init 1Service Area 2Head of service 2
532440Position 50.001.00-1.00Init 1Service Area 2Head of service 2
532620Position 60.001.00-1.00Init 1Service Area 2Head of service 2
532750Position 70.000.29-0.29Init 1Service Area 2Head of service 2
532800Position 81.001.000.00Init 1Service Area 2Head of service 2
Init 1Service Area 2Head of service 2
519100Position 10.961.00-0.04Init 2Service Area 2Head of service 2
519342Position 20.001.00-1.00Init 2Service Area 2Head of service 2
519343Position 32.000.002.00Init 2Service Area 2Head of service 2
519440Position 46.715.930.78Init 2Service Area 2Head of service 2
519460Position 51.811.750.06Init 2Service Area 2Head of service 2
519800Position 60.510.510.00Init 2Service Area 2Head of service 2
519920Position 70.781.33-0.55Init 2Service Area 2Head of service 2
Init 2Service Area 2Head of service 2
533201Position 10.600.600.00Init 3Service Area 2Head of service 2
533202Position 20.730.720.01Init 3Service Area 2Head of service 2
533203Position 32.002.000.00Init 3Service Area 2Head of service 2
533204Position 41.831.200.63Init 3Service Area 2Head of service 2
533240Position 516.6416.290.35Init 3Service Area 2Head of service 2
533245Position 61.650.001.65Init 3Service Area 2Head of service 2
533260Position 71.291.290.00Init 3Service Area 2Head of service 2
533280Position 81.000.620.38Init 3Service Area 2Head of service 2
Init 3Service Area 2Head of service 2
534001Position 10.501.00-0.50Init 4Service Area 2Head of service 2
534210Position 21.001.000.00Init 4Service Area 2Head of service 2
534320Position 31.001.000.00Init 4Service Area 2Head of service 2
534324Position 40.001.00-1.00Init 4Service Area 2Head of service 2
534340Position 53.480.622.86Init 4Service Area 2Head of service 2
534341Position 60.001.00-1.00Init 4Service Area 2Head of service 2
534342Position 70.000.90-0.90Init 4Service Area 2Head of service 2
534343Position 80.001.00-1.00Init 4Service Area 2Head of service 2
534420Position 946.3112.3134.00Init 4Service Area 2Head of service 2
534423Position 100.006.54-6.54Init 4Service Area 2Head of service 2
534424Position 110.004.23-4.23Init 4Service Area 2Head of service 2
534425Position 120.004.72-4.72Init 4Service Area 2Head of service 2
534426Position 130.007.02-7.02Init 4Service Area 2Head of service 2
534800Position 140.840.720.12Init 4Service Area 2Head of service 2
Init 4Service Area 2Head of service 2
548001Position 11.001.000.00Init 5Service Area 2Head of service 2
548211Position 22.081.001.08Init 5Service Area 2Head of service 2
548212Position 30.001.00-1.00Init 5Service Area 2Head of service 2
548400Position 45.062.462.60Init 5Service Area 2Head of service 2
548401Position 50.000.77-0.77Init 5Service Area 2Head of service 2
548411Position 63.903.890.01Init 5Service Area 2Head of service 2
548412Position 70.001.00-1.00Init 5Service Area 2Head of service 2
548413Position 80.001.00-1.00Init 5Service Area 2Head of service 2
548420Position 942.9612.6430.32Init 5Service Area 2Head of service 2
548421Position 100.001.16-1.16Init 5Service Area 2Head of service 2
548423Position 110.003.32-3.32Init 5Service Area 2Head of service 2
548432Position 120.007.01-7.01Init 5Service Area 2Head of service 2
548460Position 130.260.250.01Init 5Service Area 2Head of service 2
548620Position 140.001.55-1.55Init 5Service Area 2Head of service 2
548621Position 150.000.25-0.25Init 5Service Area 2Head of service 2
548623Position 160.000.15-0.15Init 5Service Area 2Head of service 2
548800Position 172.750.202.55Unit 5Service Area 2Head of service 2
548801Position 180.000.20-0.20Unit 5Service Area 2Head of service 2
548802Position 190.000.77-0.77Unit 5Service Area 2Head of service 2
548803Position 200.000.77-0.77Unit 5Service Area 2Head of service 2
548920Position 211.160.720.44Unit 5Service Area 2Head of service 2
Unit 5Service Area 2Head of service 2
533101Position 10.900.900.00Unit 6Service Area 2Head of service 2
533102Position 21.001.000.00Unit 6Service Area 2Head of service 2
533103Position 30.620.620.00Unit 6Service Area 2Head of service 2
533104Position 44.002.002.00Unit 6Service Area 2Head of service 2
533105Position 50.002.00-2.00Unit 6Service Area 2Head of service 2
533140Position 629.6119.649.97Unit 6Service Area 2Head of service 2
533141Position 70.0011.32-11.32Unit 6Service Area 2Head of service 2
533150Position 81.810.910.90Unit 6Service Area 2Head of service 2
533160Position 93.330.772.56Unit 6Service Area 2Head of service 2
533161Position 100.001.19-1.19Unit 6Service Area 2Head of service 2
533180Position 110.670.620.05Unit 6Service Area 2Head of service 2
Unit 6Service Area 2Head of service 2
Head of service 2If Value in Column B is in Column I then sum all values in Column C above until Value in Column I changesService Area 2Head of service 2

Which is basically what my macro in Post 20& 21 is doing, it works fine for Head of Service 1 and gives the correct total, but for Head of Service 2 it does not recognize the change in Column I and adds the Head of Service 1 total
 
Upvote 0
Try this:
I'm using the last of each Head of Service in column I to determine where the Head of Service is located in column B. I'm not sure if it fits your actual data.
VBA Code:
Sub try1()

Dim i As Long, j As Long
Dim va
Application.ScreenUpdating = False
va = Range("I1", Cells(Rows.Count, "I").End(xlUp))
For i = 2 To UBound(va, 1)
    j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
    i = i - 1
        Cells(i, "C") = WorksheetFunction.Sum(Range("C" & i - 1 & ":C" & j))
        Cells(i, "D") = WorksheetFunction.Sum(Range("D" & i - 1 & ":D" & j))
        Cells(i, "E") = WorksheetFunction.Sum(Range("E" & i - 1 & ":E" & j))
Next
Application.ScreenUpdating = True

End Sub

Example:
weefatb0b 1.xlsm
ABCDEFGHI
1Position NumberPosition TitleEstablishment CountOccupiedVacantVacant %Unit/DeptServ. AreaHOS
2Position 1123Unit 1Service Area 1Head of Service 1
3Position 2123Unit 1Service Area 1Head of Service 1
4Position 18123Unit 1Service Area 1Head of Service 1
5Position 19123Unit 1Service Area 1Head of Service 1
6Unit TotalUnit 1Service Area 1Head of Service 1
7Position 1123Unit 2Service Area 1Head of Service 1
8Position 2123Unit 2Service Area 1Head of Service 1
9Unit TotalUnit 2Service Area 1Head of Service 1
10Head of Service 161218Head of Service 1
11Head of Service 2
12Position 1123Head of Service 2
13Position 2123Head of Service 2
14Position 3123Head of Service 2
15Position 17Head of Service 2369Head of Service 2
Sheet1
 
Upvote 0
Solution
Try this:
I'm using the last of each Head of Service in column I to determine where the Head of Service is located in column B. I'm not sure if it fits your actual data.
VBA Code:
Sub try1()

Dim i As Long, j As Long
Dim va
Application.ScreenUpdating = False
va = Range("I1", Cells(Rows.Count, "I").End(xlUp))
For i = 2 To UBound(va, 1)
    j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
    i = i - 1
        Cells(i, "C") = WorksheetFunction.Sum(Range("C" & i - 1 & ":C" & j))
        Cells(i, "D") = WorksheetFunction.Sum(Range("D" & i - 1 & ":D" & j))
        Cells(i, "E") = WorksheetFunction.Sum(Range("E" & i - 1 & ":E" & j))
Next
Application.ScreenUpdating = True

End Sub

Example:
weefatb0b 1.xlsm
ABCDEFGHI
1Position NumberPosition TitleEstablishment CountOccupiedVacantVacant %Unit/DeptServ. AreaHOS
2Position 1123Unit 1Service Area 1Head of Service 1
3Position 2123Unit 1Service Area 1Head of Service 1
4Position 18123Unit 1Service Area 1Head of Service 1
5Position 19123Unit 1Service Area 1Head of Service 1
6Unit TotalUnit 1Service Area 1Head of Service 1
7Position 1123Unit 2Service Area 1Head of Service 1
8Position 2123Unit 2Service Area 1Head of Service 1
9Unit TotalUnit 2Service Area 1Head of Service 1
10Head of Service 161218Head of Service 1
11Head of Service 2
12Position 1123Head of Service 2
13Position 2123Head of Service 2
14Position 3123Head of Service 2
15Position 17Head of Service 2369Head of Service 2
Sheet1
That is exactly what I need, and works exactly as I wanted, thank you very much, I appreciate the assistance and apologies for back and forth.
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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