For without next in Excel - vba

I'm trying to get all possible combinations with a kind of VBA macro presented in https://stackoverflow.com/a/10693789/1992004, but get an error For without Next. I compared the source from another thread with mine, but don't found such difference, which could cause this error.
Do you see, what causes this error? - please point me to. My Code follows.
Option Explicit
Sub Sample()
Dim l As Long, m As Long, n As Long, o As Long, p As Long, q As Long, r As Long, s As Long, t As Long, u As Long
Dim CountComb As Long, lastrow As Long
Range("L2").Value = Now
Application.ScreenUpdating = False
CountComb = 0: lastrow = 18
For l = 1 To 1: For m = 1 To 2
For n = 1 To 2: For o = 1 To 18
For p = 1 To 15: For q = 1 To 10
For r = 1 To 10: For s = 1 To 17
For t = 1 To 3: For u = 1 To 3
Range("L" & lastrow).Value = Range("A" & l).Value & "/" & _
Range("B" & m).Value & "/" & _
Range("C" & n).Value & "/" & _
Range("D" & o).Value & "/" & _
Range("E" & p).Value & "/" & _
Range("F" & q).Value & "/" & _
Range("G" & r).Value & "/" & _
Range("H" & s).Value & "/" & _
Range("I" & t).Value & "/" & _
Range("J" & u).Value
lastrow = lastrow + 1
CountComb = CountComb + 1
Next: Next
Next: Next
Range("L1").Value = CountComb
Range("L3").Value = Now
Application.ScreenUpdating = True
End Sub

All the comments above explain your problem, but this is what your code would look like with proper indenting AND the missing "next" statements:
For l = 1 To 1
For m = 1 To 2
For n = 1 To 2
For o = 1 To 18
For p = 1 To 15
For q = 1 To 10
For r = 1 To 10
For s = 1 To 17
For t = 1 To 3
For u = 1 To 3
Range("L" & lastrow).Value = Range("A" & l).Value & "/" & _
Range("B" & m).Value & "/" & _
Range("C" & n).Value & "/" & _
Range("D" & o).Value & "/" & _
Range("E" & p).Value & "/" & _
Range("F" & q).Value & "/" & _
Range("G" & r).Value & "/" & _
Range("H" & s).Value & "/" & _
Range("I" & t).Value & "/" & _
Range("J" & u).Value
lastrow = lastrow + 1
CountComb = CountComb + 1
Next
Next
Next
Next
Next
Next
Next
Next
Next
Next
At the very least, it would have made it immediately obvious where your code was failing.

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

VBA: Adding a condition to an ElseIf loop

I'd like to add one more condition to this Loop that would put "Discrepancy" into column J if there is a value in column F that is neither 0 or #VALUE. Any and all suggestions would be appreciated. Thank you, the current code is below:
Sub ERS_Vlookup()
Dim Lastrow As Long
Dim h As Long
For h = 5 To Lastrow
If IsError(ActiveSheet.Range("H" & h).Value) Or
IsError(ActiveSheet.Range("F" & h).Value) Then
ActiveSheet.Range("J" & h).Value = " "
ElseIf ActiveSheet.Range("H" & h).Value <> " " And _
ActiveSheet.Range("F" & h).Value = 0 Then
ActiveSheet.Range("J" & h).Value = "Paid"
Else
ActiveSheet.Range("J" & h).Value = "Processed Not Yet Paid"
End If
Next h
On the face of it, this should work, but I think you need to think it through and check whether these conditions are all mutually exclusive.
Sub ERS_Vlookup()
Dim Lastrow As Long
Dim h As Long
For h = 5 To Lastrow
If IsError(Range("H" & h).Value) Or IsError(Range("F" & h).Value) Then
Range("J" & h).Value = vbNullString
ElseIf Range("H" & h).Value <> vbNullString And Range("F" & h).Value = 0 Then
Range("J" & h).Value = "Paid"
ElseIf Not IsError(Range("F" & h).Value) And Range("F" & h).Value <> 0 Then
Range("J" & h).Value = "Discrepancy"
Else
Range("J" & h).Value = "Processed Not Yet Paid"
End If
Next h
End Sub
You can the use the And function to add booleans to loops
i recomend you visit this website http://www.excel-easy.com/vba/loop.html
for more information

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

Incrementing my loops correctly

The code below calculate an average between col E, colG and col I (like in the picture in column K)but something doesn't work how I need. My code doesn't take correctly column G. It takes in the loop 5,7,3,15,10 and not 5,5,7,3,3,3,15,15,10,10. It doesn't consider how many repetitive names are in column A. How to solve the problem? Anyone is able to help? thanks!
(look at the picture)
For f= 1 To ws.Range("F" & Rows.Count).End(xlUp).Row
nameF = ws.Range("F" & f).Value
For totRng = 1 To lastrowA
'if names from col A and col F coincide, then sum their numbers from col E
If nameF = ws.Range("A" & totRng).Value Then ws.Range("G" & f).Value =
ws.Range("G" & f).Value + ws.Range("E" & totRng).Value
On Error Resume Next
If nameF = ws.Range("A" & totRng).Value Then _
ws.Range("H" & f).Value = ((ws.Range("E" & f).Value / ws.Range("G" & f).Value)) * ws.Range("I" & f).Value
Next totRng
Next f
I should change this value: '/ ws.Range("G" & f).Value)'
Here a working piece of code
For f = 2 To ws.Range("F" & Rows.Count).End(xlUp).Row
nameF = ws.Range("F" & f).Value
For totRng = 2 To lastrowA
'if names from col A and col F coincide, then sum their numbers from col E
If nameF = ws.Range("A" & totRng).Value Then
ws.Range("G" & f).Value = ws.Range("G" & f).Value + ws.Range("E" & totRng).Value
End If
Next totRng
For totRng = 2 To lastrowA
'calculate the average
If nameF = ws.Range("A" & totRng).Value Then
Debug.Print nameF & " found on line " & totRng & " person we have is on line " & f & ". Formula is : ((" & ws.Range("E" & totRng).Value & "/" & ws.Range("G" & f).Value & ")*" & ws.Range("I" & totRng).Value & "=" & (((ws.Range("E" & totRng).Value / ws.Range("G" & f).Value)) * ws.Range("I" & totRng).Value)
ws.Range("H" & f).Value = ws.Range("H" & f).Value + (((ws.Range("E" & totRng).Value / ws.Range("G" & f).Value)) * ws.Range("I" & totRng).Value)
End If
Next totRng
Next f