VBA - 运行时错误6.溢出

时间:2017-08-01 15:41:56

标签: excel vba excel-vba overflow

这是我要修复的代码。这是来自excel电子表格宏。

实施例。当我运行Tally Inspector宏并输入检查器的名称时,宏假定每周都会接收数据表单,然后填充我创建的表。但是当我运行它时,我得到运行时错误6.溢出错误。

代码:

Sub Tally Inspector()    
    Dim personName As String
    personName = InputBox("Enter Name Of Inspector")

    Dim counter As Integer

    Dim endOfTable
    endOfTable = 130

    Dim witnessSum As Integer: witnessSum = 0 'number witness by CCI
    Dim ICbyCrew As Integer: ICbyCrew = 0 'number done by crew

    Dim columnLetter As Integer: columnLetter = 11 'K

    For counter = 5 To endOfTable
        If StrComp(Cells(counter, columnLetter), personName) = 0 And _
            IsNumeric(Cells(counter, columnLetter - 1)) = True Then

            witnessSum = (witnessSum + Cells(counter, columnLetter - 1).Value)
            ICbyCrew = (ICbyCrew + Cells(counter, 4).Value)
        End If
    Next counter    

    For counter = 150 To 160 Step 1
        If Cells(counter, "E").Value = personName Then
            Cells(counter, "F").Value = ICbyCrew
            Cells(counter, "G").Value = witnessSum
            Cells(counter, "H").Value = (witnessSum / ICbyCrew)* 100 'This line is highlighted when I run the debugger'

            Exit For
        End If
    Next counter
End Sub    

Sub Inspector()

    Dim Inspector As String
    Dim Inspection As Integer: Inspection = 0
    Dim Witness As Integer: Witness = 0

    For x = 155 To 158      
        Inspector = Cells(x, "E")
        If Inspector = "" Then
            Exit For
        End If

        For y = 5 To 120
            If (StrComp(Inspector, Cells(y, "K")) = 0) And (IsNumeric(Cells(y, "D")) = True) And (IsNumeric(Cells(y, "J")) = True) Then
                Inspection = Inspection + Cells(y, "D")
                Witness = Witness + Cells(y, "J")
            End If                
        Next y

    Cells(x, "F") = Inspection
    Cells(x, "G") = Witness
    Cells(x, "H") = (Witness / Inspection) * 100

    Inspection = 0
    Witness = 0        
    Next x    
End Sub

2 个答案:

答案 0 :(得分:1)

Integer重新定义为Long。您可能已超过Integer的32,767个数字限制。

答案 1 :(得分:0)

更改突出显示的代码:

Cells(counter, "H").Value = (witnessSum / ICbyCrew) 100

到:

Cells(counter, "H").Value = (witnessSum / ICbyCrew) / 100

或者:

Cells(counter, "H") = witnessSum / ICbyCrew & 100

或者:

Cells(counter, "H") = witnessSum / ICbyCrew & " " &100