我收到错误6:溢出。为什么?

时间:2015-12-16 19:27:14

标签: excel vba

这是我程序中的一个功能。在while循环中,我正在溢出。为什么?我该如何解决?

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

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

j = 8: matchcounter = 0: runningtotal = 0

For counter = 7 To counter60M

    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

    For k = counter To (counter + matchcounter)
        runningtotal = runningtotal + Worksheets(sheetname).Cells(counter, 10)
        'here is where you do the calculations for the duration hours
    Next k

    Worksheets(sheetname).Cells(counter, 11) = runningtotal

    counter = j: j = j + 1: matchcounter = 0: runningtotal = 0


Next counter

2 个答案:

答案 0 :(得分:1)

您最有可能因一个(或组合)三个原因而得到错误:

1)您的While循环永远不会逃脱

2) counter60M是一个非常大的数字

3) matchcounter是一个非常大的数字

答案 1 :(得分:1)

更改

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

Dim j As Long, matchcounter As Long, k As Long, runningtotal As Long

然后你应该没事了