2017-08-01 384 views
0

這是我正在嘗試修復的代碼。這是來自Excel電子表格宏。VBA - 運行時錯誤6.溢出

Ex。當我運行Tally檢查器宏並鍵入檢查器的名稱時,宏將假設在每週的每一天都接收數據表單,然後填充我所做的表。但是當我運行它,我得到運行時錯誤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 
+0

你在哪一行得到錯誤? – Sam

+0

'x'和'y'完全沒有定義。其最佳做法是明確使用選項顯式和定義變量類型。 –

+0

「Cells(counter,」H「)。Value =(witnessSum/ICbyCrew)100」 - Line 34 – Adeeb

回答

1

重新定義你的IntegerLong。您可能正在運行超過Integer的32,767個數字限制。

+0

*您可能正在運行整數32,767的數字限制* - 代碼實際設置了所有定義變量的硬開始和結束點(x和y,但是沒有在任何地方定義!) –

+0

@ScottHoltzman嘿斯科特,請閱讀您的評論昨天,但不是很確定你的意思 - 謹慎闡述? – dwirony

+0

例如,在行中:'For counter = 5 To endOfTable'用戶將'endOfTable'設置爲130所定義的值,該整數將處理。在循環中使用'counter'的地方也會發生這種情況。 –

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 
+0

Cells(counter,「H」)。Value =(witnessSum/ICbyCrew)* 100 – Adeeb

+0

@Adeeb - 這個細胞究竟應該留在什麼地方? – Vityata

+0

它只是我的電子表格中計算百分比的一列。 – Adeeb