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

时间:2015-12-17 01:09:53

标签: excel vba types mismatch

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

 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

1 个答案:

答案 0 :(得分:0)

当您收到错误消息时,请单击错误对话框中的“调试”按钮。

细胞:

工作表(工作表名称)。细胞(k,10)

中包含非数字或非整数数据。您可以检查变量k的值,在工作表上找到该单元格并尝试在该单元格中写入整数。然后再次运行此子。错误应该消失。

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