I am having two sheets , sht1 and sht2. I am trying to count value in the column R, S, T, U if it contains 1 and paste them in a table in sheet2.
First, I always, look for the calendar week in sht2, it checks for the present week number, and then I check the cw printed in my sheet1, if they are equal then it count for the number of 1 in the column I have mentioned.
I am getting an overflow error in the below line
For 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 would be helpful.
Integer
toLong
... hint:Dim cntU, cntS, CntV As Integer
onlyCntV
isInteger
!cntU
andcntS
are Variant ;) – Dirk ReichelLong
is faster and uses less memory in VBA thenInteger
does) – Dirk ReichelRows.Count
toSheets("sht1").Rows.Count
. – YowE3KRows.Count
with a sheet reference. – Tim Williamsj
to beLong
instead ofInteger
? And it is definitely an overflow error on theFor j = 5 To Sheets("sht1").Cells(Rows.Count, 23).End(xlUp).Row
line? – YowE3K