Changing end integer of a loop within said loop - vb.net

I am working on some code and I am trying to modify the end integer of a loop within said loop (as said in the title)
So it would look like this
dim x as integer
For i=0 to x
if (some conditions) then
x=x+1
End if
Next
However it doesn't seem to work, it looks like the loop is working with a static value of x and not a dynamic value.
Is there a way to update the end integer of the loop without using an exit for and starting the loop all over?
Thanks

You can use a While loop.
dim x as integer
Dim i as Integer = 0
While i <= x
if (some conditions) then
x=x+1
End if
End While

Related

Even Odd numbers using Nested For Next Loop VBA

How do I create a For-Next loop determining whether the numbers listed are even or odd
You can try something like this:
Dim i As Integer
For i = 1 To 6
If i mod 2 = 0 Then
'i is even
Else
'i is odd
End If
Next i

LOOP and nested if statements not working

the code is intended to seek out the "Yes" in "OverExtensions" and will create a new range and determine the maximum value of the new range. For some reason the logic of the code seems alright but I'm getting an empty result and would love the community's input as to where I've went wrong.
Private Sub CommandButton1_Click()
Dim maximum As Double, OverExtension As Range, x As Range
Cells.Interior.ColorIndex = 0
Set OverExtension = Range("D2:D12")
maximum = WorksheetFunction.Max(Range("c2:c12"))
For Each x In OverExtension
If x.Value = Yes Then
If x.Offset(0, -1).Value = maximum Then
x.Cells.Interior.ColorIndex = 22
End If
End If
Next x
End Sub
You nested the if and for blocks in a wrong way.
You have to put the second End If between Next PriceNo and Next OEResult.
Update:
You need just one End If. The first one is wrong because If and Then are in one line.
Besides you have problem with your OEYes and OEResult variables as they are defined but not initialized but as I don't know your intention I also don't know how to solve it.

Delete row if first letter in cell is a letter

I am trying to loop through a range in column A and delete each row where the value in cell A starts with a letter (e.g. delete C159, but not 8T9G3). I think the code may work correctly, if I get the between piece straight. Any suggestions how I can get the code to work?
Sub DeleteLetterRows()
Dim k as integer
For k = 2 To 100
If Asc(ActiveSheet.Range("A" & k).value) >=65 and <=90 or >=97 and <=122
Rows(k).EntireRow.Delete
Else
End If
Next k
End Sub
Thanks!
Some issues:
When you delete a row, and k increases, you actually skip a row, which you don't check. So, better go downwards with k to avoid this problem.
The way you compare against ASCII values has wrong syntax, as you need to explicitly specify a value after and immediately before each >= and <= operator. But instead of giving a correction (which would be long), I'll suggest a shorter syntax:
Checking for a letter can be done in a more readable way, which does not require the knowledge of ASCII codes. Simply check if the first character is different when put in upper case than when put in lower case. If so, it is a letter.
You are missing the Then keyword at the end of the If line;
Code:
Sub DeleteLetterRows()
Dim k as integer
Dim l as String
For k = 100 To 2 Step -1
l = Left(ActiveSheet.Range("A" & k).value, 1)
If UCase(l) <> LCase(l) Then
Rows(k).EntireRow.Delete
End If
Next k
End Sub
To check the first character of your string you use the function Left().
The easiest way to find out if is not a number is the function isNumeric().
Put together you will get
Sub DeleteLetterRows()
Dim k as integer
Dim test As String
For k = 2 To 100
test = Left(ActiveSheet.Range("A" & k).value,1)
If isNumeric(test) = False then
Rows(k).EntireRow.Delete
Else
End If
Next k
End Sub

Iterating over a collection (VBA)

Hello I am new to VBA and I am trying to iterate over a collection i've made and execute an action for each value.
Here is my code:
Sub makeRowsMatch()
Dim rows As VBA.Collection
Set rows = New VBA.Collection
Dim i As Integer
Dim j As Integer
Dim y As Integer
For i = 2 To 22203
For j = 2 To 121
If Cells(i, 2).Value <> Cells(j, 38) Then
rows.Add (i)
End If
Next j
Next i
For Each y As Object in rows
rows(y).Delete
Next y
End Sub
I keep on getting an error in the line:
For Each y As Object in rows
It keeps on getting highlighted yellow, but when I remove the above line is get the following error:
For Each control variable must be Variant or Object
Could someone explain why it is not letting me iterate over the values?
The whole issue of declaring variables is new to me.
Thanks!
Declare y as Variant, i.e.:
Dim y As Variant
and change your loop like this:
For Each y In rows
Then the loop will work or, at least, it won't give you an error at that point.
But the next problem/error is inside the loop. This line:
rows(y).Delete
will give you an error "Object required", or something like that, because rows(y) return an Integer, so you can't call Delete on it. There's some confusion here because rows on its own means the Rows range of the active sheet. But you also named your collection as rows, so there's a naming conflict.
Rename your rows to, for example, myrows and it should then all work. However, I don't know what you're trying to do with the Delete line, so I can't advise further.
You've got a conflict in your declarations and use. This line declares y as an Integer, clearly:
Dim y As Integer
But you then try to use it as an Object, either with
For Each y As Object in rows
or
For Each y in rows
Change the declaration to
Dim y As Object
(I'm not that familiar with VBA. If the above doesn't work, change the declaration to Dim y As Variant instead.)

Nested loop with same variable

Is that possible for me to do nested loop in VB with same counter
The code is somehow like this
For a As Integer = 1 to Console.ReadLine
For a = 1 to a
Console.WriteLine("*")
Next
Console.WriteLine()
Next
The program is designed for drawing a triangle of * with just a single variable at all
VB just disallow me to use a in nested loop again
Error: ...Variable 'a' is alreay used by a independent loop.
I have my own usage, can only use 1 variable.
What changing the second FOR loop to a WHILE loop?
For a As Integer = 1 to Console.ReadLine
Do While a <=5
Console.WriteLine("Line: " & a)
Exit Do
Loop
Next
Here's a different idea. You may consider splitting your integer variable into 2 parts 16-bit parts, keep user's input in the upper 16-bits, and current iteration value in the lower 16-bits (you'll need to use WHILE instead of FOR).
In fact, what you need is to start your inner counter by the value of a, if I've understand. And what you are doing is create another loop inside starting by 1.
For a As Integer = 1 to Console.ReadLine
For b As Integer = a to 5
Console.WriteLine("Line: " & a)
Next
Next
You cannot declare another variable with the same name in the same scope. But the inner loop is in the same scope as the outer loop. That's why you get that compiler error.
You can use a different name:
Dim i As Int32
If Int32.TryParse(Console.ReadLine, i) AndAlso i > 0 Then
For a As Integer = 1 To i
For aa = 1 To i
Console.WriteLine("Line: {0} {1}", a, aa)
Next
Next
End If
To draw a triangle, as you describe, you need two variables, like this:
For a As Integer = 1 to Console.ReadLine
For b As Integer = 1 to a
Console.Write("*")
Next
Console.WriteLine()
Next
If you used the same variable in the inner loop, the inner loop would change the value of the variable, which in most cases would not be what you want, and in all cases would be incredibly confusing. For that reason, VB forces you to use a different iterator in each nested For loop.