VBA-Sum Columns Ignoring Other sheets

weefatb0b

New Member
Joined
Nov 17, 2022
Messages
28
Office Version
  1. 2013
Platform
  1. Windows
I have been asked to have a look at the attached VB to see why it is not working, and after sitting looking at it for quite a few hours, with my limited knowledge, as I am still up skilling VBA, I cannot see why it is not working.

VBA Code:
Sub SumColumns()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim cell As Range
    
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Sheet1" And ws.Name <> "Sheet2" And ws.Name <> "Sheet3" And ws.Name <> "Sheet5" Then
        ' If ws.Name <> "Headcount" And ws.Name <> "Position" And ws.Name <> "Incumbents" And ws.Name <> "Lookup" Then
            lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
            
            For i = 1 To lastRow
                Set cell = ws.Cells(i, 2)
                
                If cell.Value = "Unit Total" Then
                    cell.Offset(0, 1).End(xlToRight).Offset(0, 1).Value = _
                        WorksheetFunction.Sum(ws.Range(cell.Offset(0, 1), cell.Offset(0, 3)))
                End If
            Next i
        End If
    Next ws
End Sub

As you can see I have tried ws.Name both ways and it still does not put the totals in to the required cells.

What it is trying to do, is ignore the 4 sheets named, then for all other sheets in the workbook, look at column B and where the name in the Cell in column B = Unit Total then add up all the values in Column C, then D then E, next to Unit Total and put these values in to the last cell in columns C D & E

Hope I have explained the process well enough?

Thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
It is very hard to help without being able to see what the data in your sheets looks like.
Can you post a sample of what the data in one of these sheets looks like, and also show us what your expected results look like?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Sorry, hope this helps


Position NumberPosition TitleEstablishment CountOccupiedVacantVacant %Unit/DeptServ. AreaHOS
540100Position 11.001.000.00Unit 1Service area 1Head of Service 1
540220Position 21.001.000.00Unit 1Service area 1Head of Service 1
540221Position 31.001.000.00Unit 1Service area 1Head of Service 1
540222Position 41.001.000.00Unit 1Service area 1Head of Service 1
540440Position 52.002.000.00Unit 1Service area 1Head of Service 1
540441Position 63.003.000.00Unit 1Service area 1Head of Service 1
540442Position 71.511.510.00Unit 1Service area 1Head of Service 1
540620Position 81.001.000.00Unit 1Service area 1Head of Service 1
540621Position 91.511.510.00Unit 1Service area 1Head of Service 1
540622Position 101.001.000.00Unit 1Service area 1Head of Service 1
540623Position 110.510.510.00Unit 1Service area 1Head of Service 1
540800Position 120.590.590.00Unit 1Service area 1Head of Service 1
540920Position 130.410.410.00Unit 1Service area 1Head of Service 1
Unit Totals.15.5315.530.000.0%Unit 1Service area 1Head of Service 1
585180Position 11.450.001.45Unit 2Service area 1Head of Service 1
585181Position 20.001.00-1.00Unit 2Service area 1Head of Service 1
585185Position 30.001.00-1.00Unit 2Service area 1Head of Service 1
Unit Totals.1.452.00-0.55-37.9%Unit 2Service area 1Head of Service 1
541100Position 11.001.000.00Unit 3Service area 1Head of Service 1
541220Position 23.001.002.00Unit 3Service area 1Head of Service 1
541221Position 30.001.00-1.00Unit 3Service area 1Head of Service 1
541440Position 43.832.451.38Unit 3Service area 1Head of Service 1
541800Position 50.540.540.00Unit 3Service area 1Head of Service 1
Unit Totals.8.375.992.3828.4%Unit 3Service area 1Head of Service 1
585100Position 10.951.00-0.05Unit 4Service area 1Head of Service 1
585420Position 21.031.030.00Unit 4Service area 1Head of Service 1
Unit Totals.1.982.03-0.05-2.5%Unit 4Service area 1Head of Service 1
542100Position 11.001.000.00Unit 5Service area 1Head of Service 1
542200Position 21.000.510.49Unit 5Service area 1Head of Service 1
542201Position 30.001.00-1.00Unit 5Service area 1Head of Service 1
542220Position 41.040.380.66Unit 5Service area 1Head of Service 1
542221Position 50.000.51-0.51Unit 5Service area 1Head of Service 1
542420Position 62.201.940.26Unit 5Service area 1Head of Service 1
542800Position 70.510.510.00Unit 5Service area 1Head of Service 1
Unit Totals.5.755.85-0.10-1.7%Unit 5Service area 1Head of Service 1
Sum of each of cells in column above where Column B = Unit Totals.Sum of each of cells in column above where Column B = Unit Totals.Sum of each of cells in column above where Column B = Unit Totals.
 
Upvote 0
One thing I see right off the bat. Your code is looking for "Unit Total":
VBA Code:
If cell.Value = "Unit Total" Then
but your values are actually:
"Unit Totals."

So it would not find any matches at all.
 
Upvote 0
One thing I see right off the bat. Your code is looking for "Unit Total":
VBA Code:
If cell.Value = "Unit Total" Then
but your values are actually:
"Unit Totals."

So it would not find any matches at all.
Thanks, never noticed that, when I change it to the correct cell name, its kind of working, but its putting a value in Column J instead of the row at at the bottom, .
 
Upvote 0
Do you understand what this line is doing?
VBA Code:
cell.Offset(0, 1).End(xlToRight).Offset(0, 1).Value = ...
It is moving over one column (from B to C), then going all the way over to the right to the last column with data (column I), then moving over one more column (column J).
And that is where it is entering your formula.
It is not doing anything with columns C, D, or E.
 
Upvote 0
Do you understand what this line is doing?
VBA Code:
cell.Offset(0, 1).End(xlToRight).Offset(0, 1).Value = ...
It is moving over one column (from B to C), then going all the way over to the right to the last column with data (column I), then moving over one more column (column J).
And that is where it is entering your formula.
It is not doing anything with columns C, D, or E.
No Joe, I wasnt sure tbh, as I said I am very much at the upskilling stage and trying to learn, so would I be better advising a rewrite so they get it correct?
 
Upvote 0
Yeah, the code, as written, will not do what you want.

If you are wanting a grand total at the bottom, to sum just the lines where column B is equal to "Unit Totals.", you do not need to loop through every cell in column B.
You can just put a SUMIF formula in the last row of each sheet.
If you need help updating your code to do that, please let us know and we can probably help with that.
 
Upvote 0
If I understand your question correctly, I think this should do what you want:
VBA Code:
Sub SumColumns()
    Dim ws As Worksheet
    Dim lastRow As Long
   
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Sheet1" And ws.Name <> "Sheet2" And ws.Name <> "Sheet3" And ws.Name <> "Sheet5" Then
'           Get last row in column B
            lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
'           Insert formulas into columns C, D, and E
            ws.Cells(lastRow + 1, "C").Formula = "=SUMIF($B2:$B" & lastRow & ",""Unit Totals."",C2:C" & lastRow & ")"
            ws.Cells(lastRow + 1, "D").Formula = "=SUMIF($B2:$B" & lastRow & ",""Unit Totals."",D2:D" & lastRow & ")"
            ws.Cells(lastRow + 1, "E").Formula = "=SUMIF($B2:$B" & lastRow & ",""Unit Totals."",E2:E" & lastRow & ")"
        End If
    Next ws
End Sub
 
Upvote 0
Solution
You understood correctly and this does exactly what is required. Thanks for all your help.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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