James
James

Reputation: 499

Summing Two Columns - Type mismatch Error

I am trying to sum two columns, but i keep getting an error message of type mismatch. Where my error is?

Sub SumCols()
  Dim ws As Worksheet
  Set ws = Sheets("Recon")
  LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
  For i = 2 To LastRow
    Range("E" & i).Value = Range("C" & i).Value + Range("D" & i).Value
  Next i
End Sub

The below might be my issue, I checked for blank cells and non were found. But I can see blank cells.

enter image description here

Upvotes: 0

Views: 288

Answers (1)

braX
braX

Reputation: 11755

Most likely one of the values you are trying to add together is not numeric, so check to see if they are numeric and non-blank before you try adding them.

For i = 2 To LastRow
  If Len(Range("C" & i).Value) > 0 And Len(Range("D" & i).Value) > 0 Then
    If IsNumeric(Range("C" & i).Value) And IsNumeric(Range("D" & i).Value) Then
      Range("E" & i).Value = Range("C" & i).Value + Range("D" & i).Value
    End If
  End If
Next i

Also, you might be better off just using a formula:

Range("E" & i).Formula = "=C" & i & "+D" & i

Upvotes: 2

Related Questions