Runtime error overflow - vba

I am trying to do the following. I am having two sheets.
From sheet1, I count number of 0 according to the condition and copy it in the table in sheet2.
I am doing the same with different conditions. When I am executing the code, I am getting an runtime error, Overflow. Can someone help me what is the reason.
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

I am pretty much willing to bet that you are getting your error in this location:
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
While the suggestion posted by Sam is the first place to look (Integers have a max value of ~32,000 whereas Longs have a max value of ~2 billion), the second place to always look is divisions by 0.
While there is an error code for division by 0, you may encounter instances where you have a division by 0 that results in an overflow error. The best way to fix this is something like this:
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
While this will do the trick, if I was writing the code I would likely try to wrap this in some kind of function. You could even write a function that divides two numbers, and returns 0 if the denominator is 0. I'll leave that up to you though.
Also, I highly recommend refactoring this code. You should check out Rubberduck : http://rubberduckvba.com/. It is a fantastic tool that can get you well on your way to writing better code.
I hope this helps!

Most probably cntW = 0 and thus deletion on the line
(Sht.Range("S" & i) = cntF / cntW) is not possible, as far as it is not possible to divide by zero.
Fix your code to make sure it is not happening.
Like this:
If cntW <> 0 then Sht.Range("S" & i) = cntF / cntW
To check what is the value of cntW, write the following:
MsgBox cntW somewhere before the error.

Change all Integer variables to Long and try again

Related

Excel VBA Variable is not incrementing correctly

I have the below Excel 2016 VBA function where I am outputting data to various worksheets. I seem to be having an issue with the data on my "Error" worksheet where I am getting a lot of blank rows between other rows with data.
This issue is only occuring with the "Error" worksheet. I have tried putting the k = k + 1 variable around some and it has not helped. Is there any obvious place where I should (or shouldn't be) incrementing k that may fix the issue? Or perhaps l and j need to be changed?
Function PPDdate()
Dim PPD_1_Date As Date
Dim PPD_2_Date As Date
Dim TSpot_Date As Variant
Dim i As Long, j As Long, k As Long
j = Worksheets("PPDCI").Range("A" & Rows.Count).End(xlUp).Row + 1
l = Worksheets("CI").Range("A" & Rows.Count).End(xlUp).Row + 1
k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 2 To lstrow
PPD_1_Date = Worksheets("Data").Range("AW" & i)
PPD_2_Date = Worksheets("Data").Range("BA" & i)
Entity = Worksheets("Data").Range("J" & i)
Dept = Worksheets("Data").Range("M" & i)
TSpot_Date = Worksheets("Data").Range("AS" & i)
If PPD_1_Date > PPD_2_Date Then
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = PPD_1_Date
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("AX" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("AZ" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("AY" & i).Value
Worksheets("PPDCI").Range("J" & j).Value = "1st IF STATEMENT"
j = j + 1
Else
If PPD_1_Date < PPD_2_Date Then
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = PPD_2_Date
'Worksheets("PPDCI").Range("G" & j).Value = "ELSE IF CONDITION"
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("BB" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("BD" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("BC" & i).Value
Worksheets("PPDCI").Range("J" & j).Value = "2nd IF STATEMENT"
j = j + 1
'If IsEmpty(Worksheets("Data").Range(PPD_1_Date & i).Value) = True And IsEmpty(Worksheets("Data").Range(PPD_2_Date & i).Value) = True Then
'GoTo EmptyRange
'Else
Else
If Len(TSpot_Date) <> 0 And InStr(1, UCase(Worksheets("Data").Range("AT" & i).Value), "NEG") > 0 Then
Worksheets("CI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("CI").Range("D" & j).Value = "TSNG"
Worksheets("CI").Range("E" & j).Value = TSpot_Date
Worksheets("CI").Range("F" & j).Value = "3rd IF STATEMENT"
'j = j + 1
Else
If Len(TSpot_Date) <> 0 And InStr(1, UCase(Worksheets("Data").Range("AT" & i).Value), "POS") > 0 Then
Worksheets("CI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("CI").Range("D" & j).Value = "TSPS"
Worksheets("CI").Range("E" & j).Value = TSpot_Date
Worksheets("CI").Range("F" & j).Value = "4th IF STATEMENT"
l = l + 1
Else
If (InStr(1, Entity, "CNG Hospital") > 0 Or InStr(1, Entity, "Home Health") > 0 Or InStr(1, Entity, "Hospice") > 0 Or InStr(1, Dept, "Volunteers") > 0 And (PPD_1_Date = 0 And PPD_2_Date = 0)) And TSpot_Date = 0 Then
Worksheets("Error").Range("A" & k & ":H" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("Error").Range("F" & k).Value = "REVIEW PPD DATA"
Worksheets("Error").Range("G" & k).Value = "5th IF STATEMENT"
k = k + 1
Else
Worksheets("Error").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("Error").Range("D" & j).Value = TSpot_Date
Worksheets("Error").Range("E" & j).Value = Worksheets("Data").Range("AT" & i).Value
Worksheets("Error").Range("F" & j).Value = "REVIEW PPD/TSPOT DATA"
Worksheets("Error").Range("G" & j).Value = "6th IF STATEMENT"
'k = k + 1
End If
End If
End If
End If
End If
'EmptyRange:
'k = k + 1
Next i
End Function
Example output:
Thanks in advance!
You are setting the range in the else statement of your print to ERROR sheet with J when it should be K
Else
If (InStr(1, Entity, "CNG Hospital") > 0 Or InStr(1, Entity, "Home Health") > 0 Or InStr(1, Entity, "Hospice") > 0 Or InStr(1, Dept, "Volunteers") > 0 And (PPD_1_Date = 0 And PPD_2_Date = 0)) And TSpot_Date = 0 Then
Worksheets("Error").Range("A" & k & ":H" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("Error").Range("F" & k).Value = "REVIEW PPD DATA"
Worksheets("Error").Range("G" & k).Value = "5th IF STATEMENT"
k = k + 1
Else
Worksheets("Error").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("Error").Range("D" & j).Value = TSpot_Date
Worksheets("Error").Range("E" & j).Value = Worksheets("Data").Range("AT" & i).Value
Worksheets("Error").Range("F" & j).Value = "REVIEW PPD/TSPOT DATA"
Worksheets("Error").Range("G" & j).Value = "6th IF STATEMENT"
k = k + 1
End If
Should be
Else
If (InStr(1, Entity, "CNG Hospital") > 0 Or InStr(1, Entity, "Home Health") > 0 Or InStr(1, Entity, "Hospice") > 0 Or InStr(1, Dept, "Volunteers") > 0 And (PPD_1_Date = 0 And PPD_2_Date = 0)) And TSpot_Date = 0 Then
Worksheets("Error").Range("A" & k & ":H" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("Error").Range("F" & k).Value = "REVIEW PPD DATA"
Worksheets("Error").Range("G" & k).Value = "5th IF STATEMENT"
k = k + 1
Else
Worksheets("Error").Range("A" & k & ":C" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("Error").Range("D" & k).Value = TSpot_Date
Worksheets("Error").Range("E" & k).Value = Worksheets("Data").Range("AT" & i).Value
Worksheets("Error").Range("F" & k).Value = "REVIEW PPD/TSPOT DATA"
Worksheets("Error").Range("G" & k).Value = "6th IF STATEMENT"
k = k + 1
End If
I think that same issue can be found in your print to CI else if statement.
Also, as one of the comments states, how are you setting your last row?
You should consider declaring your worksheets, would make this cleaner IMO
As was noted in other answers, & j should have been & k. However that isn't the "root issue".
The "root issue" is poor variable naming. Take the time to name things properly, it's worth it.
Rename i to currentRowData
Rename j to currentRowPPDCI
Rename k to currentRowError
Rename l to currentRowCI
Writing code is 5% of the job. Reading code is the other 95%. Therefore, code should be written to be read, not just executed. Yes, using meaningful identifiers take - oh, a whole second more to type. But they also make the code easier to read, which makes bugs like these much easier to catch.
i is a fine identifier when you're looking at a trivial For...Next loop. Anything else deserves a real name.
l is arguably the single most horrible single-letter identifier to use, because it's easily mistaken for a 1 at a glance.
It looks to me like your final "else" clause is using the last row of the "PPDCI" worksheet instead of the "errors" worksheet--the variable j instead of k.
You are not consistent with the use of your row counters (j, k and l) in relation to your sheets (PPDCI, CI and Error)
try:
Function PPDdate()
Dim PPD_1_Date As Date
Dim PPD_2_Date As Date
Dim TSpot_Date As Variant
Dim i As Long, j As Long, k As Long
j = Worksheets("PPDCI").Range("A" & Rows.Count).End(xlUp).Row + 1
l = Worksheets("CI").Range("A" & Rows.Count).End(xlUp).Row + 1
k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 2 To lstrow
PPD_1_Date = Worksheets("Data").Range("AW" & i)
PPD_2_Date = Worksheets("Data").Range("BA" & i)
Entity = Worksheets("Data").Range("J" & i)
Dept = Worksheets("Data").Range("M" & i)
TSpot_Date = Worksheets("Data").Range("AS" & i)
If PPD_1_Date > PPD_2_Date Then
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = PPD_1_Date
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("AX" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("AZ" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("AY" & i).Value
Worksheets("PPDCI").Range("J" & j).Value = "1st IF STATEMENT"
j = j + 1
Else
If PPD_1_Date < PPD_2_Date Then
Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("PPDCI").Range("F" & j).Value = PPD_2_Date
'Worksheets("PPDCI").Range("G" & j).Value = "ELSE IF CONDITION"
Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("BB" & i).Value
Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("BD" & i).Value
Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("BC" & i).Value
Worksheets("PPDCI").Range("J" & j).Value = "2nd IF STATEMENT"
j = j + 1
Else
If Len(TSpot_Date) <> 0 And InStr(1, UCase(Worksheets("Data").Range("AT" & i).Value), "NEG") > 0 Then
Worksheets("CI").Range("A" & l & ":C" & l).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("CI").Range("D" & l).Value = "TSNG"
Worksheets("CI").Range("E" & l).Value = TSpot_Date
Worksheets("CI").Range("F" & l).Value = "3rd IF STATEMENT"
l = l + 1
Else
If Len(TSpot_Date) <> 0 And InStr(1, UCase(Worksheets("Data").Range("AT" & i).Value), "POS") > 0 Then
Worksheets("CI").Range("A" & l & ":C" & l).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("CI").Range("D" & l).Value = "TSPS"
Worksheets("CI").Range("E" & l).Value = TSpot_Date
Worksheets("CI").Range("F" & l).Value = "4th IF STATEMENT"
l = l + 1
Else
If (InStr(1, Entity, "CNG Hospital") > 0 Or InStr(1, Entity, "Home Health") > 0 Or InStr(1, Entity, "Hospice") > 0 Or InStr(1, Dept, "Volunteers") > 0 And (PPD_1_Date = 0 And PPD_2_Date = 0)) And TSpot_Date = 0 Then
Worksheets("Error").Range("A" & k & ":H" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("Error").Range("F" & k).Value = "REVIEW PPD DATA"
Worksheets("Error").Range("G" & k).Value = "5th IF STATEMENT"
k = k + 1
Else
Worksheets("Error").Range("A" & k & ":C" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
Worksheets("Error").Range("D" & k).Value = TSpot_Date
Worksheets("Error").Range("E" & k).Value = Worksheets("Data").Range("AT" & i).Value
Worksheets("Error").Range("F" & k).Value = "REVIEW PPD/TSPOT DATA"
Worksheets("Error").Range("G" & k).Value = "6th IF STATEMENT"
End If
End If
End If
End If
End If
Next i
End Function

Outputting to two worksheets

I have the below function that outputs records to either a worksheet called CI or one called Error. I added an additional IF statement where if my source 'col' column contains the word "TITER" then I want it to output to the "Error" worksheet. This seems to be working and outputting the appropriate records to the Error tab. However I noticed that it is also outputting these same records to the "CI" worksheet as well. I have the IF code nested in the main Else statement, but I'm thinking it doesn't belong there. Any help is appreciated!
Public lstrow As Long, strDate As Variant, stredate As Variant
Sub importbuild()
lstrow = Worksheets("Data").Range("G" & Rows.Count).End(xlUp).Row
Function DateOnlyLoad(col As String, col2 As String, colcode As String)
Dim i As Long, j As Long, k As Long
j = Worksheets("CI").Range("A" & Rows.Count).End(xlUp).Row + 1
k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 2 To lstrow
strDate = spacedate(Worksheets("Data").Range(col & i).Value)
stredate = spacedate(Worksheets("Data").Range(col2 & i).Value)
If (Len(strDate) = 0 And (col2 = "NA" Or Len(stredate) = 0)) Or InStr(1, UCase(Worksheets("Data").Range(col & i).Value), "EXP") > 0 Then
GoTo EmptyRange
Else
If InStr(1, UCase(Worksheets("Data").Range(col & i).Value), "TITER") > 0 Then
Worksheets("Error").Range("A" & k & ":C" & k).Value = Worksheets("Data").Range("F" & i & ":H" & i).Value
Worksheets("Error").Range("D" & k).Value = "REVIEW MMR1 DATES"
k = k + 1
End If
Worksheets("CI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("F" & i & ":H" & i).Value
Worksheets("CI").Range("D" & j).Value = colcode
Worksheets("CI").Range("E" & j).Value = datecleanup(strDate)
Worksheets("CI").Range("L" & j).Value = dateclean(strDate)
Worksheets("CI").Range("M" & j).Value = strDate
If col2 <> "NA" Then
If IsEmpty(stredate) = False Then
Worksheets("CI").Range("F" & j).Value = datecleanup(stredate)
End If
End If
j = j + 1
End If
EmptyRange:
Next i
End Function
Please review and compare to original code. You can see the quick change made. Indentation helps so much to spot errors and/or opportunities to improve the code.
Function DateOnlyLoad(col As String, col2 As String, colcode As String)
Dim i As Long, j As Long, k As Long
j = Worksheets("CI").Range("A" & Rows.Count).End(xlUp).Row + 1
k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 2 To lstrow
strDate = spacedate(Worksheets("Data").Range(col & i).Value)
stredate = spacedate(Worksheets("Data").Range(col2 & i).Value)
If (Len(strDate) = 0 And (col2 = "NA" Or Len(stredate) = 0)) Or InStr(1, UCase(Worksheets("Data").Range(col & i).Value), "EXP") > 0 Then
GoTo EmptyRange
Else
If InStr(1, UCase(Worksheets("Data").Range(col & i).Value), "TITER") > 0 Then
Worksheets("Error").Range("A" & k & ":C" & k).Value = Worksheets("Data").Range("F" & i & ":H" & i).Value
Worksheets("Error").Range("D" & k).Value = "REVIEW MMR1 DATES"
k = k + 1
Else
Worksheets("CI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("F" & i & ":H" & i).Value
Worksheets("CI").Range("D" & j).Value = colcode
Worksheets("CI").Range("E" & j).Value = datecleanup(strDate)
Worksheets("CI").Range("L" & j).Value = dateclean(strDate)
Worksheets("CI").Range("M" & j).Value = strDate
If col2 <> "NA" Then
If IsEmpty(stredate) = False Then
Worksheets("CI").Range("F" & j).Value = datecleanup(stredate)
End If
End If
j = j + 1
End If
End If
EmptyRange:
Next i
End Function

Irregularity with the looping

I have a sheet "result" and another sheet "status".
The sheet "status" has a table with column A as calendar week.
The idea is to copy the count values of column K, of the "result" sheet to the data sheet.
Similarly other count values in other columns as well.
The code is working fine with no errors.
The problem is I get an error with two of my values.
CntT acc.to code is providing me with "93" actually , I have "95"
similarly cntS acc to code is providing me "49" while actually I have "50".
Could anyone help me to figure out where I am wrong.I already posted this question and checked with the feedback. It dint work for me.
Sub result()
Dim i As Integer
Dim j As Integer
Dim cnt As Integer
Dim cntU As Integer
Dim sht As Worksheet
Dim TotalRows As Long
Set sht = Sheets("Status")
Sheets("Result").Select
TotalRows = Range("E5").End(xlDown).Row
n = Worksheets("Result").Range("E5:E" & TotalRows).Cells.SpecialCells(xlCellTypeConstants).Count
For i = 2 To WorksheetFunction.Count(sht.Columns(1))
cntT = 0
cntU = 0
cntS = 0
If sht.Range("A" & i) = Val(Format(Now, "WW")) Then Exit For
Next i
For j = 4 To WorksheetFunction.CountA(Columns(17))
If sht.Range("A" & i) = Range("Q" & j) And Range("K" & j) = "Green" Then cntT = cntT + 1
If sht.Range("A" & i) = Range("Q" & j) And Range("J" & j) = "delayed" Then cntU = cntU + 1
If sht.Range("A" & i) = Range("Q" & j) And Range("A" & j) = "" Then cntS = cntS + 1
If cntT <> 0 Then sht.Range("C" & i) = cntT
If cntU <> 0 Then sht.Range("D" & i) = cntU
If cntS <> 0 Then sht.Range("B" & i) = cntS
If n <> 0 Then sht.Range("E" & i) = n
Next j
If cntT + cntU <> 0 Then
sht.Range("F" & i) = (cntS / n)
sht.Range("G" & i) = (cntT / n)
sht.Range("H" & i) = (cntS / n)
sht.Range("G" & i & ":F" & i & ":H" & i).NumberFormat = "0.%"
End If
'sht.Range("G" & i & ":F" & i & ":H" & i).NumberFormat = "0.%"
End Sub

Nested Loops & Ifs, loop without Do

I keep getting a Loop without Do every time I run my code and I do not see where I am missing anything or if a loop is misplaced.
I need this code to find key words in specific columns copy then paste them into a summary tab.
Your help would be greatly appreciated.
Sub Summary()
Dim MainLoop As Double
Dim SecondLoop As Double
Dim thirdLoop As Double
Dim Trow As Double
Dim counter As Integer
Dim PSKU As Integer
Dim PDesc As String
Dim PPKG As Integer
Dim CSKU As Integer
Dim CDesc As String
Dim CPKG As Integer
Dim Cstatus As String
MainLoop = 5
SecondLoop = 0
thirdLoop = 0
Trow = 5
counter = 0
Worksheets("final").Activate
Do While MainLoop < ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
Worksheets("Final").Activate
ParentSKU = Range("F" & MainLoop).Value
ParentDesc = Range("G" & MainLoop).Value
Worksheets("Summary").Activate
SumRow = (ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row) + 1
Range("A" & SumRow).Value = ParentSKU
Range("B" & SumRow).Value = ParentDesc
Range("C" & SumRow).Value = "Parent"
Worksheets("Final").Activate
Do While SecondLoop < 20
If Range("H" & MainLoop + SecondLoop).Value = "MAT" Or "PKG" Or "ING" Then
Range("F" & MainLoop + SecondLoop).Value = CSKU
Range("G" & MainLoop + SecondLoop).Value = CDesc
Range("H" & MainLoop + SecondLoop).Value = (Cstatus)
Range("I" & MainLoop + SecondLoop).Value = CPKG
Worksheets("Summary").Activate
SumRow = (ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row) + 1
Range("A" & SumRow).Value = CSKU
Range("B" & SumRow).Value = CDesc
Range("C" & SumRow).Value = "Child"
Range("D" & SumRow).Value = CPKG
ElseIf Range("H" & MainLoop + SecondLoop).Value = "WIP" Then
Find = Range("F" & MainLoop + SecondLoop).Value
Do While Trow < ActiveSheet.Cells(Rows.Count, "J").End(xlUp).Row & thirdLoop < 20
If Range("J" & Trow).Value = Find Then
If Range("P" & Trow + thirdLoop).Value <> "" Then
CSKU = Range("P" & Trow + thirdLoop).Value
CDesc = Range("Q" & Trow + thirdLoop).Value
Cstatus = Range("R" & Trow + thirdLoop).Value
CPKG = Range("S" & Trow + thirdLoop).Value
Worksheets("Summary").Activate
SumRow = (ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row) + 1
Range("A" & SumRow).Value = CSKU
Range("B" & SumRow).Value = CDesc
Range("C" & SumRow).Value = "Child"
Range("D" & SumRow).Value = CPKG
Worksheets("Final").Activate
thirdLoop = thirdLoop + 1
Trow = Trow + 1
Else
Trow = Trow + 1
End If
Else
thirdLoop = thirdLoop + 1
End If
Loop
End If
SecondLoop = SecondLoop + 1
MainLoop = MainLoop + 20
Loop
Worksheets("Final").Activate
End Sub
The following section is missing an End If
If Range("P" & Trow + thirdLoop).Value <> "" Then
CSKU = Range("P" & Trow + thirdLoop).Value
CDesc = Range("Q" & Trow + thirdLoop).Value
Cstatus = Range("R" & Trow + thirdLoop).Value
CPKG = Range("S" & Trow + thirdLoop).Value
Worksheets("Summary").Activate
SumRow = (ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row) + 1
Range("A" & SumRow).Value = CSKU
Range("B" & SumRow).Value = CDesc
Range("C" & SumRow).Value = "Child"
Range("D" & SumRow).Value = CPKG
Worksheets("Final").Activate
thirdLoop = thirdLoop + 1
Trow = Trow + 1
'missing end if here
Loop

For...Next loop breaks when using Not() operator

I am running a for...next loop that checking whether entries in a dataset meet a certain condition (in this case IsNA). However, changing the if-then-else conditions within this loop to also check whether a condition is not met seems to break the for/next loop. I receive a Next without For error even though that element of the sub hasn't changed.
I'm lost as to why the it thinks there is no next in the for loop when that part of the code hasn't changed.
--Original Working Code--
Option Explicit
Dim i As Double
Dim a As Range
Public ssht As Worksheet
Public susht As Worksheet
Public mdsht As Worksheet
Public LastRow As Long
Dim testcell As Long
Public Sub MissingDataSetCopy()
'Part Bii
'Find rows with NA error
Application.ScreenUpdating = False
Dim i, j As Integer
j = 4
'Finds current range on Summary worksheet
Set ssht = ThisWorkbook.Worksheets("sandbox")
Set mdsht = ThisWorkbook.Worksheets("MissingData")
Set susht = ThisWorkbook.Worksheets("summary")
'Copies data to sandbox sheet as values
susht.UsedRange.copy
ssht.Range("A1").PasteSpecial (xlPasteValues)
LastRow = ssht.Range("A4").CurrentRegion.Rows.Count
Dim testcell As Double
Dim numchk As Boolean
'For...Next look call ISNUMBER test
For i = 860 To 874
If Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) Then
mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i - 1 & ":G" & i - 1).Value
j = j + 1
mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value
j = j + 1
mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i + 1 & ":G" & i + 1).Value
j = j + 1
End If
Next i
Dim fnd As Variant
Dim rplc As Variant
fnd = "#N/A"
rplc = "=NA()"
mdsht.Cells.Replace what:=fnd, Replacement:=rplc, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
End Sub
--Edit to If Statements--
For i = 860 To 874
If Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) And Application.WorksheetFunction.IsNA(ssht.Range("B" & i - 1)) Then
mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value
j = j + 1
If Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) And Not (Application.WorksheetFunction.IsNA(ssht.Range("B" & i - 1))) Then
mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i - 1 & ":G" & i - 1).Value
j = j + 1
mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value
j = j + 1
End If
Next i
You need to close the second If block:
For i = 860 To 874
If Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) And Application.WorksheetFunction.IsNA(ssht.Range("B" & i - 1)) Then
mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value
j = j + 1
End If '<-- it was not closed
If Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) And Not (Application.WorksheetFunction.IsNA(ssht.Range("B" & i - 1))) Then
mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i - 1 & ":G" & i - 1).Value
j = j + 1
mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value
j = j + 1
End If
Next i
Or alternatively using the ElseIf keyword if the two conditions (at it seems) are excluding each other:
For i = 860 To 874
If Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) And Application.WorksheetFunction.IsNA(ssht.Range("B" & i - 1)) Then
mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value
j = j + 1
ElseIf Application.WorksheetFunction.IsNA(ssht.Range("B" & i)) And Not (Application.WorksheetFunction.IsNA(ssht.Range("B" & i - 1))) Then
mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i - 1 & ":G" & i - 1).Value
j = j + 1
mdsht.Range("B" & j & ":H" & j).Value = ssht.Range("A" & i & ":G" & i).Value
j = j + 1
End If
Next i