我有两张Sheets sheet1和sheet2
在sheet1中,在AX列中,我使用公式打印了本周。我正在寻找sheet1,T列和U列,并计算两列中1'S的数量。
两列中1的计数值应粘贴在sheet2中,查看AX列中sheet1的周。如果列AX的周数为24,那么它应该沿着sheet2,A列运行24,并在B列中粘贴T的Count值,在C列中粘贴U的Count值,并计算两者的百分比,并在C中粘贴和D.
我试过一个代码,我经常把它当作0,我被打到了我错的地方。我既不检查计数也不检查周数。
Sub test()
Dim col As Range
Dim row As Range
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim T As Integer
Dim U As Integer
Dim wk As String
Set sh1 = Sheets("BW")
Set sh2 = Sheets("Results")
For Each col In sh2.Columns 'This loops through all populated columns in row one
If sh2.Cells(1, col.Column).Value = "" Then
Exit For
End If
wk = sh2.Cells(1, col.Column).Value
For Each rw In sh1.Rows
If sh1.Cells(rw.row, 50).Value = "" Then
Exit For
End If
If sh1.Cells(rw.row, 50) = wk And sh1.Cells(rw.row, 20) = 1 Then
T = T + 1
End If
If sh1.Cells(rw.row, 50) = wk And sh1.Cells(rw.row, 21) = 0 Then
U = U + 1
End If
Next rw
sh2.Cells(2, col.Column) = T 'put counters into 2nd and 3rd row under each week, you can adjust this to put the number in a different cell.
sh2.Cells(3, col.Column) = U
T = 0 'reset counters to start looking at next week.
U = 0
Next col
End Sub
答案 0 :(得分:2)
从问题中可以看出,A栏中所示给定周的表格“结果”显示在B栏和B栏中。列T&中的C计数为1。分别是其他表格的U.
解决此问题的一种方法是,对于“结果”表中的每一行,计数器查看“AX”栏中指示的那一周的“BW”表的所有行,以从T列和T列获得计数。 Ú
这会给出一些想法:
Sub test()
Dim i As Integer, j As Integer, cntT As Integer, cntU As Integer, ws As Worksheet
Set ws = Sheets("Results")
Sheets("BW").Select
For i = 2 To WorksheetFunction.CountA(ws.Columns(1))
If ws.Range("A" & i) = Val(Format(Now, "ww")) Then Exit For
Next i
ws.Range("B" & i & ":" & "E" & i).ClearContents
cntT = 0
cntU = 0
For j = 5 To WorksheetFunction.CountA(Columns(50))
If ws.Range("A" & i) = Range("AX" & j) And Range("AA" & j) <> "" Then
If Range("T" & j) = 1 Then cntT = cntT + 1
If Range("U" & j) = 1 Then cntU = cntU + 1
End If
Next j
If cntT <> 0 Then ws.Range("B" & i) = cntT
If cntU <> 0 Then ws.Range("C" & i) = cntU
If cntT + cntU <> 0 Then
ws.Range("D" & i) = cntT / (cntT + cntU)
ws.Range("E" & i) = cntU / (cntT + cntU)
End If
ws.Range("D" & i & ":E" & i).NumberFormat = "0%"
End Sub
更新:根据后续与@Mikz的讨论,以下标准添加到上述更新代码中: