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:
Head of Service 1 value is in Column B and the lookup column is column I
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
what I have been asked for is to only sum column C if the value in column B is in Column I,
Head of Service 1 value is in Column B and the lookup column is column I
But in image in post #5, col I is blank?
Sorry, I'm confused. Can you explain what you're trying to do using the sample & show what the result should look like?
 
Upvote 0
Sorry my bad, copy and paste error, this is what it does look like

Position NumberPosition TitleEstablishment CountOccupiedVacantVacant %Unit/DeptServ. AreaHOS
1234567Position 10.930.000.93Unit 1Service Area 1Head of Service 1
1234567Position 20.000.96-0.96Unit 1Service Area 1Head of Service 1
1234567Position 32.100.771.33Unit 1Service Area 1Head of Service 1
1234567Position 40.001.00-1.00Unit 1Service Area 1Head of Service 1
1234567Position 50.000.05-0.05Unit 1Service Area 1Head of Service 1
1234567Position 61.620.621.00Unit 1Service Area 1Head of Service 1
1234567Position 70.001.00-1.00Unit 1Service Area 1Head of Service 1
1234567Position 81.001.000.00Unit 1Service Area 1Head of Service 1
1234567Position 94.700.004.70Unit 1Service Area 1Head of Service 1
1234567Position 100.000.41-0.41Unit 1Service Area 1Head of Service 1
1234567Position 110.002.41-2.41Unit 1Service Area 1Head of Service 1
1234567Position 120.000.41-0.41Unit 1Service Area 1Head of Service 1
1234567Position 133.270.872.40Unit 1Service Area 1Head of Service 1
1234567Position 140.000.87-0.87Unit 1Service Area 1Head of Service 1
1234567Position 150.000.87-0.87Unit 1Service Area 1Head of Service 1
1234567Position 160.000.87-0.87Unit 1Service Area 1Head of Service 1
1234567Position 171.030.001.03Unit 1Service Area 1Head of Service 1
1234567Position 180.000.41-0.41Unit 1Service Area 1Head of Service 1
1234567Position 190.620.000.62Unit 1Service Area 1Head of Service 1
Unit 1Service Area 1Head of Service 1
1234567Position 11.041.14-0.10Unit 2Service Area 1Head of Service 1
1234567Position 20.080.31-0.23Unit 2Service Area 1Head of Service 1
1234567Position 33.001.002.00Unit 2Service Area 1Head of Service 1
1234567Position 40.001.00-1.00Unit 2Service Area 1Head of Service 1
1234567Position 50.001.00-1.00Unit 2Service Area 1Head of Service 1
1234567Position 66.281.294.99Unit 2Service Area 1Head of Service 1
1234567Position 70.000.000.00Unit 2Service Area 1Head of Service 1
1234567Position 80.001.81-1.81Unit 2Service Area 1Head of Service 1
1234567Position 90.000.51-0.51Unit 2Service Area 1Head of Service 1
1234567Position 101.810.870.94Unit 2Service Area 1Head of Service 1
1234567Position 110.000.87-0.87Unit 2Service Area 1Head of Service 1
1234567Position 120.330.49-0.16Unit 2Service Area 1Head of Service 1
1234567Position 130.770.000.77Unit 2Service Area 1Head of Service 1
1234567Position 140.770.380.39Unit 2Service Area 1Head of Service 1
Unit 2Service Area 1Head of Service 1
Head of Service 1Sum Here if B37 is in column ISum Here if B37 is in column ISum Here if B37 is in column I

and then loop all the way to the bottom of the data where each value in Column B i.e. HOS Name is in Column I. The Blank rows will be completed with a Macro in Step 2 , I want to do the first bit first, so the sum does not include the Unit Totals, as this would give an inflated total.
 
Upvote 0
It doesnt, there will be various other Heads of Service, there is over 750 roes of data,
 
Upvote 0
It doesnt, there will be various other Heads of Service, there is over 750 roes of data,
Do you mean that in column B, there are multiple instances of "Head of Service"? And for every row that contains "Head of Service" (in col B), you need to perform the summation as you described?

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"?"
 
Upvote 0
Sorry, I really havent explained this very well. I will try again, my thinking is for every value in Column B, where it equals the value in column I then sum up all the values in Column C, from the row above value in column B and put value in Cell next to value in Column B I have anonomised the data, these will not be the values actually on my report, and I have not included all 800 odd rows, but between each Head of Service row in Column B there will be data to be summed. Hopefully this is a bit clearer,


Position NumberPosition TitleEstablishment CountOccupiedVacantVacant %Unit/DeptServ. AreaHOS
Position 10.930.000.93Unit 1Service Area 1Head of Service 1
Position 20.000.96-0.96Unit 1Service Area 1Head of Service 1
Position 32.100.771.33Unit 1Service Area 1Head of Service 1
Position 40.001.00-1.00Unit 1Service Area 1Head of Service 1
Position 50.000.05-0.05Unit 1Service Area 1Head of Service 1
Position 61.620.621.00Unit 1Service Area 1Head of Service 1
Position 70.001.00-1.00Unit 1Service Area 1Head of Service 1
Position 81.001.000.00Unit 1Service Area 1Head of Service 1
Position 94.700.004.70Unit 1Service Area 1Head of Service 1
Position 100.000.41-0.41Unit 1Service Area 1Head of Service 1
Position 110.002.41-2.41Unit 1Service Area 1Head of Service 1
Position 120.000.41-0.41Unit 1Service Area 1Head of Service 1
Position 133.270.872.40Unit 1Service Area 1Head of Service 1
Position 140.000.87-0.87Unit 1Service Area 1Head of Service 1
Position 150.000.87-0.87Unit 1Service Area 1Head of Service 1
Position 160.000.87-0.87Unit 1Service Area 1Head of Service 1
Position 171.030.001.03Unit 1Service Area 1Head of Service 1
Position 180.000.41-0.41Unit 1Service Area 1Head of Service 1
Position 190.620.000.62Unit 1Service Area 1Head of Service 1
Unit TotalUnit 1Service Area 1Head of Service 1
Position 11.041.14-0.10Unit 2Service Area 1Head of Service 1
Position 20.080.31-0.23Unit 2Service Area 1Head of Service 1
Position 33.001.002.00Unit 2Service Area 1Head of Service 1
Position 40.001.00-1.00Unit 2Service Area 1Head of Service 1
Position 50.001.00-1.00Unit 2Service Area 1Head of Service 1
Position 66.281.294.99Unit 2Service Area 1Head of Service 1
Position 70.000.000.00Unit 2Service Area 1Head of Service 1
Position 80.001.81-1.81Unit 2Service Area 1Head of Service 1
Position 90.000.51-0.51Unit 2Service Area 1Head of Service 1
Position 101.810.870.94Unit 2Service Area 1Head of Service 1
Position 110.000.87-0.87Unit 2Service Area 1Head of Service 1
Position 120.330.49-0.16Unit 2Service Area 1Head of Service 1
Position 130.770.000.77Unit 2Service Area 1Head of Service 1
Position 140.770.380.39Unit 2Service Area 1Head of Service 1
Unit TotalUnit 2Service Area 1Head of Service 1
Head of Service 1Head of Service 1
Head of Service 2Head of Service 2
Head of Service 3Head of Service 3
Head of Service 4Head of Service 4
Head of Service 5Head of Service 5
Head of Service 6Head of Service 6
Head of Service 7Head of Service 7
Head of Service 8Head of Service 8
Head of Service 9Head of Service 9
Head of Service 10Head of Service 10
 
Upvote 0
Ive managed to get the below working

Dim ws As Worksheet
Set ws = Worksheets("Position")
ws.Range("C52") = Application.WorksheetFunction.SUMIF(ws.Range("I2:I51"), "*" & ws.Range("B52") & "*", ws.Range("C2:C51"))
End Sub

But this only does it for cell C52
 
Upvote 0
I've also tried this:

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(xlDown)))
End If
Next i
End Sub


But this returns the value 1 in each of the cells in C adjacent to the Value in B :-(
 
Upvote 0
So I have managed to get the below working to an extent

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

However It does not recognize where the Value match between column B & I changes and includes the total row of the match above?
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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