简体   繁体   中英

I am getting error 6: overflow. Why?

This is a function that is in my program. In the while loop, i'm getting an overflow. Why? How do i fix this?

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

You are most likely getting that error for one (or combination) of three reasons:

1) Your While loop never escapes

2) counter60M is an extremely large number

3) matchcounter is an extremely large number

Change

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

To

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

then you should be fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM