Invalid Next Control Variable - excel

I am hitting a wall with the error "Compile Error: Invalid Next Control variable reference".
Trying to skip code lines if the variable is not found in the sheet, e.g if X = 0 then skip. Can't debug step by step since it jumps straight to the error.
appreciate any 2 cents
Dim a As Integer
Dim b As Integer
Dim D As Integer
'{.......}
' a
Freight_Str_Minimum_List(1) = strFreightperMinCol:
Freight_Str_Minimum_List(2) = strFreightMinCol:
Freight_Str_Minimum_List(3) = strMOtherMinCol
' b
Freight_Col_Minimum_List(1) = FreightperMinCol:
Freight_Col_Minimum_List(2) = FreightMinCol:
Freight_Col_Minimum_List(3) = MOtherMinCol
' d
Freight_Str_PerKg_List(1) = strFreightperKgCol:
Freight_Str_PerKg_List(2) = strFreightperkgAll_InCol:
Freight_Str_PerKg_List(3) = strFreightless45All_InCol:
Freight_Str_PerKg_List(4) = strFreightgreater45All_InCol:
Freight_Str_PerKg_List(5) = strFreight100All_InCol:
' this goes until (9)
For a = 1 To 3
For b = 1 To 3
For D = 1 To 9
If Freight_Str_Minimum_List(a) = 0 Then GoTo nextsegment3
If Freight_Col_Minimum_List(b) = 0 Then GoTo nextsegment3
If Freight_Col_PerKg_List(D) = 0 Then GoTo nextsegment3
'lines of code with calculations With...end with
nextsegment3:
Next a
Next b
Next D

No Goto:
For a = 1 To 3
For b = 1 To 3
For D = 1 To 9
If Freight_Str_Minimum_List(a) <> 0 Then
If Freight_Col_Minimum_List(b) <> 0 Then
If Freight_Col_PerKg_List(D) <> 0 Then
'lines of code with calculations With...end with
End If
End If
End If
Next D
Next b
Next a

Related

For Loop with If-Or Statement VBA

I have hard-coded for ever two cells in an excel column to count if either the first or second have a value of 1. It worked this way (but also written out for every cell applicable so is inefficent):
If Range("D10").Value = 1 Or Range("D11").Value = 1 Then
x = x + 1
Else
x = x + 0
End If
I am trying to refactor it. My thought was to do a for loop, with an if and an or statement (similar to in my current code), but receive a syntax error. My attempt:
Sub refactor()
Dim x As Integer
x = 0
For Each c In Range("D2-D13").Cells
If c.Value =1 OR (c+1).Value = 1 Then
x = x + 1
Else
x = x + 0
End If
Range("A19") = x
End Sub
All I can find is for loops with if statements in them, but never the added "or" condition considered. VBA is new to me. Is there a way I can code this?
for loop in enough to run entire range itn't required "or c.offset(1,0).value = 1" because "or" operator used to any one condition is true it will allow to run the code.
"if c.Value = 1 and c.Offset(1, 0).Value = 1 Then" = this case is used for if both the statement is true then only if condition allow to run the code. are u looking for this?
Sub refactor()
Dim x As Integer
Dim c As Range
x = 0
For Each c In Range("D2:D13")
'if c.Value = 1 Or c.Offset(1, 0).Value = 1 Then'this line is same as below line
if c.value = 1 then
x = x + 1
End If
Next c
Range("A19") = x
End Sub

For Loops to perform a Step of 3 only after inserting a value into 6 rows of cells

The following code I have Will paste a value code of "01" to a cell and then skip 4 rows continuously, until reaching the end of count within the for loop. I want to run a similar loop for "02", but rather than "Step" (skip) 4 rows, I would like it to insert the value in 6 consecutive rows within the same column and then skip 3 rows continuously until reaching the end of count. I am 2 weeks new to vba, so I hope I am explaining this correctly.
Dim i As Long
If Sheet3.Range("C22").Value = "01" Then
For i = 3 To 202 Step 4
ActiveWorkbook.Sheets("CrewEntries").Cells(i, 6).Value = _
ActiveWorkbook.Sheets("MonData").Cells(22, 5).Value
Next i
ElseIf Sheet3.Range("C22").Value = "02" Then
For i = 3 To 152
ActiveWorkbook.Sheets("CrewEntries").Cells(i, 6).Value = _
ActiveWorkbook.Sheets("MonData").Cells(22, 5).Value
Next i
End If
Maybe like this:
Dim i As Long, v
v = ActiveWorkbook.Sheets("MonData").Cells(22, 5).Value
If Sheet3.Range("C22").Value = "01" Then
For i = 3 To 202 Step 4
ActiveWorkbook.Sheets("CrewEntries").Cells(i, 6).Value = v
Next i
ElseIf Sheet3.Range("C22").Value = "02" Then
For i = 3 To 152 Step 9 '6 filled + 3 empty = 9
ActiveWorkbook.Sheets("CrewEntries").Cells(i, 6).Value = v
Next i
End If
For such a kind of question, I would advise a while-loop, as in this piece of pseudo-code:
dim condition as boolean
dim loop_step, interesting_value as integer
condition = true
loop_step = 1 'just in order to be sure that it never is 0, this might create an infinite loop
interesting_value = 0 ' or some other initialisation value
while condition do
if <some_first_condition>
then
do_first_thing(interesting_value, ...)
loop_step = 3
else
do_second_thing(interesting_value, ...)
loop_step = 6
end if
interesting_value = interesting_value + loop_step
if <some_other_condition> then condition = false
Wend
Sub EarningCode()
Dim CpID As String
Dim i As Long
Dim p As Long
CpID = ActiveWorkbook.Sheets("MonData").Cells(22, 3).Value
For i = 3 To 452
If p = 9 Then
p = 1
Else
p = p + 1
End If
If p < 7 Then
ThisWorkbook.Worksheets("CrewEntries").Cells(i, 4).Value = "02"
End If
Next i
End Sub

VBA: Dynamic range count function

I want to get this table:
C|O 1 2 3 Count
A 1 1 0 2
B 0 0 1 1
C 0 0 0 0
However, with the code I have been developing I get the following result. The number of columns and rows are dynamic.
C|O 1 2 3 Count
A 1 1 0 2
B
C
Here's the code. I think the first problem is on the countRange. And the second problem is when I put the count value, I want to do as initial values b = 0 and k = 1 as well I want to advance a = a + 1 and repeat the While cycle.
Dim a As Integer
a = 0
Dim b As Integer
b = 0
Dim k As Integer
k = 1
operations = 1
Do While operations <= sh1.Range("D4").Value + 1
If sh2.Cells(12,3+b) = "Count" Then
Dim countRange As Range
Set countRange = sh2.Range(Cells(13+a, 3),Cells(13+a,3+b-1))
Dim count As Integer
count = sh2.Application.WorksheetFunction.Sum(countRange)
sh2.Cells(13+a,3+b) = count
a = a + 1
b = 0
k = 1
Else
If sh2.Cells(12+a,3+b) = operations Then
If sh2.Cells(13+a,2) = arrayC(k) Then
sh2.Cells(13+a,3+b).Value = 1
Else
sh2.Cells(13+a,3+b).Value = 0
End If
End If
operations = operations + 1
b = b + 1
k = k + 1
End If
Wend
Declare the operations variable as Dim operations as Integer = 1 and use while loop. the Do....While loop surely executes at least for single time.

Autofill increments when given a string

I've beeing searching around for quite a while and trying.
What I want to do, is basically an auto-fill that only increments when it finds a value of "02:00" on the column F
1 00:15 
1 00:45 
1 01:00 
1 01:15 
1 01:30 
1 01:45 
1 02:00  -
2 00:15 
2 00:45 
2 01:00 
2 01:15 
2 01:30 
2 01:45 
2 02:00 -
3 00:15 
3 00:45 
3 01:00 
3 01:15 
3 01:30 
3 01:45 
3 02:00 
The code I've does it almost right but always end up filling the column with the last value of the iterator.
'Days :D
i = 0
For Each c In Range("F57:F77")
For Each x In Range("E57:F77")
x.Value = i
If c.Value = "02:00 " Then
i = i + 1
If i >= 4 Then
'Exits when overlap
Exit Sub
End If
Debug.Print i
End If
Next
Next
Have you considered using a native worksheet function like COUNTIF for a bulk operation?
With Worksheets("Sheet7")
With .Range("F57", .Cells(Rows.Count, "F").End(xlUp))
.Offset(0, -1).Formula = "=COUNTIF(F$57:F57, ""02:00"")+1"
.Cells = .Value
End With
End With
How about:
Sub qwerty()
Dim x As Long, r As Range
x = 1
For Each r In Range("F57:F77")
r.Offset(0, -1).Value = x
If r.Text = "02:00" Then x = x + 1
Next r
End Sub

Dynamically adding nested loops

I have an 'X' amount of variables (likely to range between 3 to 20 options), which will be combined to calculate all possible combinations to meet a criteria. For every extra variable an extra loop is introduced, however I do not know if it possible to make the creation of loops dynamic (in excel VBA, the code doesn't have to be very fast).
To demonstrate:
I have var. A with h = 2, var. B with h = 3.
I would like to know all combinations which are equal to 10 or the best combination of the 2 variables.
In this case: option 1 = 5*A = 10, 3*B = 9,2*A and 2*B = 10, 3*A and 1*B = 9.
The code looks like this:
For A = 0 to 5
h = 0 'Reset previous h if solution is found
For B = 0 to 5
h_test = A * height(A) + B * heigth(B)
if h_test > 10
if h = 0 then
exit for
else
write h
exit for
end if
h = h_test
Next B
Next A
If another parameter is introduced (for example C = 4), the code is:
For A = 0 to 5
h = 0 'Reset previous h if solution is found
For B = 0 to 5
h = 0 'Reset previous h if solution is found
For C = 0 to 5
h_test = A * height(A) + B * heigth(B) + C * heigth(C)
if h_test > 10
if h = 0 then
exit for
else
write h
exit for
end if
h = h_test
Next C
Next B
Next A
In other words, I would like to know if it is possible to translate the pseudocode to real code:
For #parameter. = X
For loop1 = 1 to 5
h = 0
For loop2 = 1 to 5
h = 0
....
For loopX = 1 to 5
h_test = loop1 *parameter1 + loop2 * parameter 2 ...
+ loopX * parameter X
If h_test > 10
Somecode
exit for
End if
Next X
...
Next loop2
Next loop1
There are two distinct problems here. You didn't mention the first, and that is you also need to calculate a value with an indeterminate number of arguments. For that, you can use a ParamArray.
For example:
Public Function Sum(ParamArray args() As Variant) As Long
Dim i As Long
Dim operand As Integer
Dim result As Long
For i = LBound(args) To UBound(args)
result = args(i) + result
Next i
Sum = result
End Function
Which can be used and tested like this:
Public Sub Test()
Debug.Print Sum(1,2) '=> 3
Debug.Print Sum(1,2,3) '=> 6
End Sub
So, that takes care of that problem. Now, as for the problem you asked about, we'll take a similar approach. The key is to loop once for each argument you've received.
Public Sub Run()
NestedLoop 1, 2, 3
End Sub
Public Sub NestedLoop(ParamArray args() As Variant)
Dim result As Long
Dim a As Variant
a = args
Dim h_test As Long
Dim i As Long, j As Long
For i = LBound(args) To UBound(args)
For j = 1 To 5
result = 0
h_test = Sum(a)
If h_test > 10 Then
If result = 0 Then
Exit For
Else
Debug.Print result
Exit For
End If
End If
result = h_test
Next j
Next i
End Sub
Public Function Sum(args As Variant) As Long
Dim i As Long
Dim operand As Integer
Dim result As Long
For i = LBound(args) To UBound(args)
result = args(i) + result
Next i
Sum = result
End Function

Resources