duberry
duberry

Reputation: 41

SumIf value always returns as 0 - Excel VBA

I am using the SumIf function to add up all the cells that contain the string dailyTotal to the left of them. I then set a cell C29 on the Macros sheet to that value and compare it to a number to see what code executes next. I have values in the E column cells, but the value always returns as 0.

Code:

dailyTotal = "Daily Total:"
Dim totalHours As Double
Dim rng As Range: Set rng = Application.Range("Totals!E11:E100")
Dim cell As Range

For Each cell In rng.Cells
  With Sheets("Totals")
    Sheets("Macros").Range("C29").Formula = Application.SumIf(cell.Offset(, -1), dailyTotal, "")
    Sheets("Macros").Range("C29") = totalHours
  End With
Next cell

' check if command is applicable
If totalHours > 4 Then

Upvotes: 0

Views: 652

Answers (1)

user3598756
user3598756

Reputation: 29421

you may be after this

Dim totalHours As Double
Dim dailyTotal As String: dailyTotal = "Daily Total:"
Dim rng As Range: Set rng = Application.Range("Totals!E11:E100")

totalHours = Application.SumIf(rng.Offset(, -1), dailyTotal, rng)
Sheets("Macros").Range("C29") = totalHours

' check if command is applicable
If totalHours > 4 Then ...

Upvotes: 1

Related Questions