Irregularity with the looping - vba

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

Related

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

Runtime error overflow

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

VBA Sum up values in row based on condition

I need to write a VBA code based on conditions:
-if orders have the same value in column D, column F, column P and column P = unit then sum up values in column Q; if column P=amount then sum up values in column S.
Dim lastrow1 As Long
Dim startrow As Long
Dim Cumulative As Variant
Dim y As Long
With Wb2.Worksheets.Item(1)
lastrow1 = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Cumulative = 0
startrow = 4 'Row where your data starts + 1, so row 3 + 1 in this case
For y = startrow To lastrow1
If Range("P" & y - 1).Value = "Unit" Then
Cumulative = Cumulative + Range("Q" & y - 1).Value
If Range("F" & y).Value = Range("F" & y - 1).Value And Range("D" & y).Value = Range("D" & y - 1).Value And Range("P" & y).Value = Range("P" & y - 1).Value Then
Range("Q" & y - 1 & .Rows.Count).End(xlUp).Value = Cumulative
Cumulative = 0
End If
ElseIf Range("P" & y - 1).Value = "Amount" Then
Cumulative = Cumulative + Range("S" & y - 1).Value
If Range("F" & y).Value = Range("F" & y - 1).Value And Range("D" & y).Value = Range("D" & y - 1).Value And Range("P" & y).Value = Range("P" & y - 1).Value Then
Range("S" & y - 1 & .Rows.Count).End(xlUp).Value = Cumulative
Cumulative = 0
End If
End If
Next y
End With
But the code doesn't work, I got the range object error 1004 in line Range("S" & y - 1 & .Rows.Count).End(xlUp).Value = Cumulative.
Where is the problem in my code?
This is wrong Range("S" & y - 1 & .Rows.Count).End(xlUp).Value = Cumulative
It evaluates to something like for example S9:1048576 , which will throw error 1004.
Change it to :
Range("S" & y - 1 & ":S" & .Rows.Count).End(xlUp).Value = Cumulative

how to initialise my counter in vba excel

I have a problem with my vba project.
My workbook has 4 sheets (Draft, cky, coy and bey), in the sheet "draft i have all my data and i want to reorganise them. the columns "G" of the sheet "draft" contains the values (cky, coy and bey).
I want my macro to go through the colums and copy all the cells that have the same value and paste them in their corresponding sheet starting at the cell (A2), for exemple: i want the macro to copy all the data that have "cky" and paste it in the sheet "cky" starting at the cell A2 and so on/
Below you can see what i have done so far:
Sub MainPower()
Dim lmid As String
Dim srange, SelData, ExtBbFor As String
Dim lastrow As Long
Dim i, j, k As Integer
lastrow = ActiveSheet.Range("B30000").End(xlUp).Row
srange = "G1:G" & lastrow
SelData = "A1:G" & lastrow
For i = 1 To lastrow
If InStr(1, LCase(Range("E" & i)), "bb") <> 0 Then
Range("G" & i).Value = Mid(Range("E" & i), 4, 3)
ElseIf Left(Range("E" & i), 1) = "H" Then
Range("G" & i).Value = Mid(Range("E" & i), 7, 3)
Else
Range("G" & i).Value = Mid(Range("E" & i), 1, 3)
End If
Next i
'Sorting data
Range("A1").AutoFilter
Range(SelData).Sort key1:=Range(srange), order1:=xlAscending, Header:=xlYes
'Spreading to the appropriate sheets
j = 1
For i = 1 To lastrow
If Range("G" & i).Value = "CKY" Then
Sheets("CKY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
ElseIf Range("G" & i).Value = "BEY" Then
Sheets("BEY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
ElseIf Range("G" & i).Value = "COY" Then
Sheets("COY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
End If
j = j + 1
Next i
End Sub
Thank you to help
best regards
Use this refactored code in the For Loop and it should work for better for you:
For i = 1 To lastrow
Select Case Sheets("Draft").Range("G" & i).Value
Case is = "CKY","COY","BEY"
Dim wsPaste as Worksheet
Set wsPaste = Sheets(Range("G"& i).Value)
Dim lRowPaste as Long
lRowPaste = wsPaste.Range("A" & .Rows.COunt).End(xlup).Offset(1).Row
wsPaste.Range("A" & lRowPaste & ":E" & lRowPaste).Value = _
Sheets("Draft").Range("C" & i & ":G" & i).Value
End Select
Next i

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