我正在尝试执行以下操作。我有两张床单。
从sheet1开始,我根据条件计数0,并将其复制到sheet2的表中。
我在不同条件下做同样的事情。当我执行代码时,我收到运行时错误,溢出。有人可以帮我解释是什么原因。
Sub result()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim cnt As Integer
Dim cntU As Integer
Dim Sht As Worksheet
Dim totalrows As Long
Set Sht = Sheets("CTT")
Sheets("Sheet1").Select
totalrows = Range("A5").End(xlDown).Row
n = Worksheets("Sheet1").Range("A5:A" & totalrows).Cells.SpecialCells(xlCellTypeConstants).Count
For i = 2 To WorksheetFunction.Count(Sht.Columns(1))
cntT = 0
cntU = 0
Cnts = 0
cntV = 0
cntZ = 0
cntW = 0
cntA = 0
Cntb = 0
cntC = 0
cntD = 0
cntE = 0
cntF = 0
If Sht.Range("A" & i) = Val(Format(Now, "WW")) Then Exit For
Next i
For j = 5 To Sheets("Sheet1").Cells(Rows.Count, 17).End(xlUp).Row
If Sht.Range("A" & i) = Range("W" & j) And Range("Q" & j) = "D" Then cntT = cntT + 1
If Sht.Range("A" & i) = Range("W" & j) And Range("Q" & j) = "K" Then cntU = cntU + 1
If Sht.Range("A" & i) = Range("W" & j) And Range("Q" & j) = "A" Then Cnts = Cnts + 1
If Sht.Range("A" & i) = Range("W" & j) And Range("Q" & j) = "M" Then cntV = cntV + 1
If Sht.Range("A" & i) = Range("W" & j) And Range("Q" & j) = "C" Then cntW = cntW + 1
If Sht.Range("A" & i) = Range("W" & j) And Range("Q" & j) = "E" Then cntZ = cntZ + 1
If cntU <> 0 Then Sht.Range("K" & i) = cntU
If Cnts <> 0 Then Sht.Range("B" & i) = Cnts
If cntT <> 0 Then Sht.Range("E" & i) = cntT
If n <> 0 Then Sht.Range("T" & i) = n
If cntV <> 0 Then Sht.Range("N" & i) = cntV
If cntZ <> 0 Then Sht.Range("H" & i) = cntZ
If cntZ <> 0 Then Sht.Range("Q" & i) = cntW
Next j
For k = 5 To Sheets("CTT_Report").Cells(Rows.Count, 17).End(xlUp).Row
If Sht.Range("A" & i) = Range("W" & k) And Range("Q" & k) = "A" And Range("U" & k) = "0" Then cntA = cntA + 1
If Sht.Range("A" & i) = Range("W" & k) And Range("Q" & k) = "D" And Range("U" & k) = "0" Then Cntb = Cntb + 1
If Sht.Range("A" & i) = Range("W" & k) And Range("Q" & k) = "E" And Range("U" & k) = "0" Then cntC = cntC + 1
If Sht.Range("A" & i) = Range("W" & k) And Range("Q" & k) = "K" And Range("U" & k) = "0" Then cntD = cntD + 1
If Sht.Range("A" & i) = Range("W" & k) And Range("Q" & k) = "M" And Range("U" & k) = "0" Then cntE = cntE + 1
If Sht.Range("A" & i) = Range("W" & k) And Range("Q" & k) = "C" And Range("U" & k) = "0" Then cntF = cntF + 1
If cntA <> 0 Then Sht.Range("C" & i) = cntA
If Cntb <> 0 Then Sht.Range("F" & i) = Cntb
If cntC <> 0 Then Sht.Range("I" & i) = cntC
If cntD <> 0 Then Sht.Range("L" & i) = cntD
If cntE <> 0 Then Sht.Range("O" & i) = cntE
If cntF <> 0 Then Sht.Range("R" & i) = cntF
Next k
If cntA + Cnts + Cntb + cntC + cntD + cntE + cntF + cntT + cntU + cntV + cntZ <> 0 Then
Sht.Range("D" & i) = cntA / Cnts
Sht.Range("G" & i) = Cntb / cntT
Sht.Range("J" & i) = cntC / cntZ
Sht.Range("M" & i) = cntD / cntU
Sht.Range("P" & i) = cntE / cntV
Sht.Range("S" & i) = cntF / cntW
End If
End Sub
答案 0 :(得分:1)
我非常愿意打赌你在这个地方收到错误:
If cntA + Cnts + Cntb + cntC + cntD + cntE + cntF + cntT + cntU + cntV + cntZ <> 0 Then
Sht.Range("D" & i) = cntA / Cnts
Sht.Range("G" & i) = Cntb / cntT
Sht.Range("J" & i) = cntC / cntZ
Sht.Range("M" & i) = cntD / cntU
Sht.Range("P" & i) = cntE / cntV
Sht.Range("S" & i) = cntF / cntW
End If
虽然Sam发布的建议是第一个看的地方(整数的最大值为~32,000,而Longs的最大值约为20亿),但总是看的第二位是除以0。
虽然有一个除以0的错误代码,但您可能会遇到除以0的情况,导致溢出错误。解决这个问题的最佳方法是:
If cntA + Cnts + Cntb + cntC + cntD + cntE + cntF + cntT + cntU + cntV + cntZ <> 0 Then
If Cnts <> 0 Then
Sht.Range("D" & i).value = cntA / Cnts
Else
Sht.Range("D" & i).value = 0
End If
If cntT <> 0 Then
Sht.Range("G" & i).value = Cntb / cntT
Else
Sht.Range("G" & i).value = 0
End If
If cntZ <> 0 Then
Sht.Range("J" & i).value = cntC / cntZ
Else
Sht.Range("J" & i).value = 0
End If
If cntU <> 0 Then
Sht.Range("M" & i).value = cntD / cntU
Else
Sht.Range("M" & i).value = 0
End If
If cntV <> 0 Then
Sht.Range("P" & i).value = cntE / cntV
Else
Sht.Range("P" & i).value = 0
End If
If cntW <> 0 Then
Sht.Range("S" & i).value = cntF / cntW
Else
Sht.Range("S" & i).value = 0
End If
End If
虽然这可以解决问题,但如果我编写代码,我可能会尝试将其包装在某种功能中。你甚至可以编写一个除以两个数字的函数,如果分母为0,则返回0.我会把它留给你。
此外,我强烈建议重构此代码。你应该查看Rubberduck:http://rubberduckvba.com/。这是一个很棒的工具,可以帮助您更好地编写更好的代码。
我希望这有帮助!
答案 1 :(得分:1)
最有可能cntW = 0
因此删除就行了
(Sht.Range("S" & i) = cntF / cntW
)是不可能的,只要不能除以零。
修复您的代码以确保它没有发生。
像这样:
If cntW <> 0 then Sht.Range("S" & i) = cntF / cntW
要检查cntW
的值是什么,请写下以下内容:
MsgBox cntW
。
答案 2 :(得分:0)
将所有Integer变量更改为Long并再试一次