类型不匹配错误 - 似乎无法修复

Type Mismatch error - can't seem to fix it

这是我程序中的一个函数。我在下面指出的位置收到类型不匹配错误。我不确定为什么会这样。我确保添加的两个术语都是数字类型。

 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

当您收到错误消息时,请单击错误对话框中的 "Debug" 按钮。

单元格:

工作sheets(sheetname).Cells(k, 10)

其中有非数字或非整数数据。您可以检查变量 k 的值,在 sheet 上找到该单元格并尝试在该单元格中写入一个整数。然后再运行这个子。错误应该消失。

如果您出于某些业务逻辑原因必须在该单元格中保留非整数值,那么您可能需要修改您的子算法。