Search code examples
excelvbatypesmismatch

Type Mismatch error - can't seem to fix it


This is a function in my program. I am getting a type mismatch error where I have indicated below. I'm not sure why this is the case. I ensured that both terms being added are of number type.

 Sub durationhours(ByVal sheetname As String, ByVal counter60M As Integer)

Dim j As Integer, matchcounter As Integer, k As Integer, runningtotal As Integer, counter As Integer

j = 8: matchcounter = 0: runningtotal = 0

For counter = 7 To (counter60M - 1)

    While Worksheets(sheetname).Cells(counter, 2) = Worksheets(sheetname).Cells(j, 2)
        j = j + 1: matchcounter = matchcounter + 1
    Wend

If IsEmpty(Worksheets(sheetname).Cells(j, 2)) Then j = j + 3

k = counter

While k <= (counter + matchcounter)

runningtotal = runningtotal + (Worksheets(sheetname).Cells(k, 10)) 'ERROR HERE


'here is where you do the calculations for duration hours
k = k + 1
Wend

If matchcounter > 0 Then
counter = j
Worksheets(sheetname).Cells(counter, 11) = runningtotal
j = j + 1: matchcounter = 0: runningtotal = 0

Else
Worksheets(sheetname).Cells(counter, 11) = runningtotal
counter = j: j = j + 1: matchcounter = 0: runningtotal = 0 

End If

Next counter



End Sub

Solution

  • When you receive the error message click the "Debug" button on the error dialogue.

    The cell:

    Worksheets(sheetname).Cells(k, 10)

    has non-numeric or non-integer data in it . You can check the value of the variable k , find that cell on the sheet and try writing an integer number in that cell. Then run this sub again. The error should dissappear.

    If you have to keep the non-integer value in that cell for some business logic reasons, then you might need to revise your sub's algorithm.