skip over a value in a loop and then continue to loop? - vba

I need to be able to run a loop starting at 0, have it identify a certain value (in this case piecenumblack) and then skip that value, and then continue the loop until it hits 11. I'm really unsure what type of loop to use and I've had no success with a Do, While, or For loop.
Dim piecenumblack As Integer
For i = 0 To piecenumblack
Next
For i = 11 To piecenumblack Step -1
Next

You could add an If to the inside of the loop:
Dim piecenumblack As Integer
piecenumblack = 3
For i = 0 To 11
If i <> piecenumblack then
'Do Code
End If
Next
This would then skip doing any code when i = 3 then continue on with 4,5,6..11.

Related

DataGridView looping from bottom up

When I run code below, the selected row returned is always from the bottom up. How can I get it to loop from the top of the Data Grid table each time?
'Find the selected customer by code. Display closest match in grid.
Dim targetString As String = txtAccountCode.Text
For Each row As DataGridViewRow In frmCustomerLookUp.GSCUSTDataGridView.Rows
If row.Cells(0).Value.ToString().StartsWith(targetString) Then
frmCustomerLookUp.GSCUSTDataGridView.ClearSelection()
frmCustomerLookUp.GSCUSTDataGridView.Rows(row.Index).Selected = True
frmCustomerLookUp.GSCUSTDataGridView.FirstDisplayedScrollingRowIndex = frmCustomerLookUp.GSCUSTDataGridView.SelectedRows(0).Index
Dim selectedIndex = frmCustomerLookUp.GSCUSTDataGridView.SelectedRows(0).Index
frmCustomerLookUp.GSCUSTDataGridView.Rows(selectedIndex).Selected = True
frmCustomerLookUp.GSCUSTDataGridView.Rows(selectedIndex).Cells(0).Selected = True
Exit Sub
End If
Next
Use a For...Next loop, starting at the last row and stepping -1
For index As Integer = frmCustomerLookUp.GSCUSTDataGridView.Rows.Count - 1 To 0 Step -1 ' Or Count -2
If frmCustomerLookUp.GSCUSTDataGridView.Rows(index).Cells(0).Value.ToString().StartsWith(targetString) Then
As the other person said. (Just a bit shorter)
For x=frmCustomerLookUp.GSCUSTDataGridView.Rows.Count - 1 To 0 Step -1
if frmCustomerLookUp.GSCUSTDataGridView(0,x).ToString().StartsWith(targetString) then
'something
end if
next

Change a For Loop to a Do While statement?

Module Module1
Sub Main()
For value As Integer = 9 To 0 'Step 1'
System.Console.WriteLine(value)
Next
End Sub
End Module
This should be simple id imagine, how could this easily be changed from a "For Loop" to a "Do While" statement?
You can use the following Do While loop instead:
Dim value As Integer = 9
Do While value <= 9 And value >= 0
Console.WriteLine(value)
value -= 1 'this count from 9 to 0
Loop
But your For doesn't work this way. At the moment you For loop doesn't output any value. It looks like you set the wrong steps:
For value As Integer = 9 To 0 Step -1
Console.WriteLine(value) 'this count from 9 to 0
Next
The Do While loop solution to count from 0 to 9:
Dim value As Integer = 0
Do While value <= 9 And value >= 0
Console.WriteLine(value)
value += 1 'this count from 0 to 9
Loop
The For loop solution to count from 0 to 9:
For value As Integer = 0 To 9 Step 1
Console.WriteLine(value) 'this count from 0 to 9
Next
This will never enter the loop, as the start value is higher than the end, and the step is not negative. See here:
https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/for-next-statement#technical-implementation
To turn this into a Do-While, you would have to first check if the start and end conditions are valid, otherwise it would be stuck in an infinite loop.

VBA Override Error 6 Overflow (Create Infinite Loop)?

I want an infinite loop in VBA (I have done this in Java and C++ before). I keep getting "Overflow" with the VBCritical red circle X.
Here is my code. The Error <>0 is supposed to recognize the overflow and ignore it to let the macro continue looping infinitely, but I still get the overflow VBCritical MsgBox.
I want to print the numbers out in column A. This part works right now: it prints "2".
Here is my code:
Sub InfiniteLoop()
Dim counter As Integer
counter = 1
Do While counter > 0
counter = counter + 1
Loop
If Error <> 0 Then
Do While counter > 0
counter = counter + 1
Cells(counter, "A").Value = counter
Loop
End If
End Sub
For a loop that will run from 0..32767 repeatedly (the max an integer can hold)
Do While True
counter = (counter + 1) Mod 32768
...
Loop
If you dim counter as a Long the maximum will be 2147483647.
Could you not just do this? and not increment counter at all?
Sub InfiniteLoop()
Dim counter As Integer
counter = 1
Do While counter > 0
Loop
End Sub

Don't go into this loop if the array is empty

Til now the method I use to avoid going into for loops which loop through an array which is currently empty is the following:
if len(join(array,"") > 0 then
for i = 1 to ubound(array)
code
next
end if
But this is no joke, I just recently used that line of code if len(join(array,"") > 0 then and that caused the deletion of array and it crashed my program 5 times in a row. I know that sounds hard to believe but here is a screen shot
for some reason the code len(join(array,"") > 0 would destroy the variables2 array. And here is a screen shot that shows that the variables array is clearly full before I go to the bad code: So now I'm trying to use a different code I tried:
if not isempty(array) then
But that's not working. Any ideas?
If Len(Join(greek_act, "")) > 0 Then
For i = 1 To UBound(greek_act)
For j = 1 To UBound(variables2)
If greek_act(i) = variables2(j) Then
variables2(j) = ""
Exit For
End If
Next
Next
variables2 = remove_blanks(variables2)
End If
'variables2 array is full
If Len(Join(greek_letters, "")) > 0 Then
'variables2 array gets destroyed and program crashes here.
For i = 1 To UBound(greek_letters)
rightres1 = Replace(rightres1, greek_letters(i), variables2(i))
Next
End If
Never mind, I decided to just go ahead with the following solution since it appears that the program is temporarily acting up
On Error Resume Next
For i = 1 To UBound(greek_letters)
rightres1 = Replace(rightres1, greek_letters(i), variables2(i))
Next
On Error GoTo 0``
For the Join() technique to work reliably, you must complete the Dimming/ReDimming process:
Sub dural()
Dim greek_ary(1 To 3) As String
s = Join(greek_ary, "")
MsgBox Len(s)
End Sub
Without filling the array, the Len will report 0
Since this is a common test I like to use a reusable function like this:
Function IsArrayEmpty(anArray As Variant)
Dim i As Integer
On Error Resume Next
i = UBound(anArray, 1)
If Err.Number = 0 Then
IsVarArrayEmpty = False
Else
IsVarArrayEmpty = True
End If
End Function
Now in your main Sub do this:
If Not IsArrayEmpty(greek_act) Then
For i = 1 To UBound(greek_act)
For j = 1 To UBound(variables2)
If greek_act(i) = variables2(j) Then
variables2(j) = ""
Exit For
End If
Next
Next
variables2 = remove_blanks(variables2)
End If

For Loop Step -1

I want to know why this loop doesn't show anything in VB.NET.
I think this code will create an infinite loop. But it doesn't show anything.
Dim i as Integer
For i = 1 to 3 Step - 1
MessageBox.Show(i)
Next
Is that loop different with this code (in java/c#) ?
for(int i = 1;i <= 3;i--)
{
// print i
}
http://msdn.microsoft.com/en-us/library/5z06z1kb.aspx
With a negative step size the loop only executes if counter >= end. So in this case with i = 1, that is less than the ending value so the loop doesn't execute at all.
It doesn't show anything because you are running the counter backwards without reversing the start and end conditions.
Think of the loop like this:
Dim counter As Int32 = 1
Do
If counter <= 1 Then
Exit Do
End If
Console.WriteLine("The counter is at " & counter)
counter +=1
Loop
Obviously this won't work properly.
You need to reverse the start and end conditions:
For counter = 3 To 1 Step -1
Console.WriteLine("counter: " & counter)
Next
For i = 1 to 3 Step - 1
It won't create an infinite loop. The loop will simply be skipped, because you can't get from 1 to 3 with a step value of -1.
Is that loop different with this code (in java/c#) ?
This loop will also end immediately, because the initial value (i = 1) meets the exit condition (i <= 3).