I get type mismatch error in for loop in VBA

Viewed 71
'find the total of ages and anticor level to find the average

For i = 1 To lastCellNumber
    totalAge = totalAge + Cells(i, 4)
    totalAnticorLevel = totalAnticorLevel + Cells(i, 6)
Next i
2 Answers

Using FunThomas's suggestion:

'find the total of ages and anticor level to find the average

For i = 1 To lastCellNumber
    if IsNumeric(Cells(i, 4).value) as sNumeric(totalAge) then
      totalAge = totalAge + Cells(i, 4)
    end if
   if IsNumeirc(totalAnticorLevel) and IsNumeric(Cells(i, 6).value) then
    totalAnticorLevel = totalAnticorLevel + Cells(i, 6)
    end if
Next i

But that would not calculate the cases where one of the parcels is not numeric, which would be your next problem to solve.

When you do

totalAge + Cells(i, 4)

You're adding a value (totalAge) with a CELL (Cells(i, 4)). You need to use the VALUE of the cell like:

totalAge + Cells(i, 4).value
Related