我有两张纸,sht1和sht2。我试图计算R,S,T,U列中的值,如果它包含1并将它们粘贴到sheet2中的表中。
首先,我总是在sht2中查找日历周,检查当前的周数,然后检查我的sheet1中打印的cw,如果它们相等则计算列中的1的数量我已经提到了。
我在下面的行中出现溢出错误
对于j = 5 To Sheets(“sht1”)。Cells(Rows.Count,23).End(xlUp).Row
Sub result()
Dim i As Integer
Dim j As Integer
Dim cnt As Integer
Dim cntU, cntS, CntV As Integer
Dim Sht As Worksheet
Dim totalrows As Long
Set Sht = Sheets("sht2")
Sheets("sht1").Select
totalrows = Range("A5").End(xlDown).Row
n = Worksheets("sht1").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
If Sht.Range("A" & i) = Val(Format(Now, "WW")) Then Exit For
Next i
For j = 5 To Sheets("sht1").Cells(Rows.Count, 23).End(xlUp).Row
If Sht.Range("A" & i) = Range("W" & j) And Range("R" & j) = "1" Then cntT = cntT + 1
If Sht.Range("A" & i) = Range("W" & j) And Range("S" & j) = "1" Then cntU = cntU + 1
If Sht.Range("A" & i) = Range("W" & j) And Range("T" & j) = "1" Then cntS = cntS + 1
If Sht.Range("A" & i) = Range("W" & j) And Range("U" & j) = "1" Then CntV = CntV + 1
If cntU <> 0 Then Sht.Range("D" & i) = cntU
If cntS <> 0 Then Sht.Range("E" & i) = cntS
If cntT <> 0 Then Sht.Range("C" & i) = cntT
If n <> 0 Then Sht.Range("B" & i) = n
If CntV <> 0 Then Sht.Range("F" & i) = CntV
Next j
If cntT + cntU + cntS + CntV <> 0 Then
Sht.Range("G" & i) = CntV / n
Sht.Range("H" & i) = cntS / n
Sht.Range("I" & i) = cntU / n
Sht.Range("J" & i) = cntT / n
End If
End Sub
anylead会有所帮助。
答案 0 :(得分:1)
我不太确定问题中指出的行n = Worksheets("sht1").Range("A5:A" & totalrows).Cells.SpecialCells(xlCellTypeConstants).Count
。
除此之外,您的代码块似乎还有一些问题。要弄清楚这些,最好看一下下面更新的代码,该代码在当前周的1
列中单独计算R, S, T, U
,将结果粘贴到sht2
中相应的一周,并计算他们在总馅饼中的份额。
如果您正在寻找,请告诉我。
Sub result()
Dim i As Long, j As Long, cntR As Long, cntS As Long, cntT As Long, cntU As Long, Sht As Worksheet
Set Sht = Sheets("sht2")
Sheets("sht1").Select
For i = 2 To WorksheetFunction.CountA(Sht.Columns(1))
If Sht.Range("A" & i) = Val(Format(Now, "ww")) Then Exit For
Next i
Sht.Range("C" & i & ":" & "J" & i).ClearContents
For j = 5 To WorksheetFunction.CountA(Columns("W"))
If Sht.Range("A" & i) = Range("W" & j) Then
If Range("R" & j) = 1 Then cntR = cntR + 1
If Range("S" & j) = 1 Then cntS = cntS + 1
If Range("T" & j) = 1 Then cntT = cntT + 1
If Range("U" & j) = 1 Then cntU = cntU + 1
End If
Next j
If cntR <> 0 Then Sht.Range("C" & i) = cntR
If cntS <> 0 Then Sht.Range("D" & i) = cntS
If cntT <> 0 Then Sht.Range("E" & i) = cntT
If cntU <> 0 Then Sht.Range("F" & i) = cntU
If cntR + cntS + cntT + cntU <> 0 Then
Sht.Range("G" & i) = cntR / (cntR + cntS + cntT + cntU)
Sht.Range("H" & i) = cntS / (cntR + cntS + cntT + cntU)
Sht.Range("I" & i) = cntT / (cntR + cntS + cntT + cntU)
Sht.Range("J" & i) = cntU / (cntR + cntS + cntT + cntU)
End If
Sht.Range("G" & i & ":J" & i).NumberFormat = "0%"
End Sub