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
 
You are welcome.
Glad I was able to help.

If you have any questions regarding my code, please let me know.
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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