Reputation: 591
I am having two Sheets sheet1 and sheet2
In sheet1 , in column AX I have my present week printed using an formula. I am looking for the sheet1, column T and U and Count the number of 1'S in both the columns.
the counted number of 1's of both the columns should be pasted in sheet2 looking into the week of sheet1 in column AX. if the column AX has week number 24, then it should run along the sheet2, column A for 24 and paste the Count value of T in column B and Count value of U in column C, and calculate the percentage for both and Paste in C and D.
I tried through a code, I am often getting it as 0,I am struck where I am wrong. Ist neither checking the Count nor weeks.
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
Upvotes: 2
Views: 209
Reputation: 1500
It appears from the question that sheet "Results" for a given week indicated in column A, shows in column B & C count of 1's in column T & U of the other sheet respectively.
One approach to solve this is that for each row in "Results" sheet, the counter looks into all rows of "BW" sheet for that week indicated in column "AX" to get the count from column T & U.
This'll give some idea:
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
Update: as per subsequent discussion with @Mikz below criteria added to the above updated code:
Upvotes: 2