VBA Override Error 6 Overflow (Create Infinite Loop)? - vba

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

Related

VBA for...next loop with byte data type not supported?

Why do I get error 6 "overflow" with this code? I'm confused..
Sub test()
Dim i as byte
For i = 3 To 2 step - 1
Debug.Print i
Next
End Sub
The same with type integer works.
In this case, the step is also of type Byte and bytes can only be 0 ... 255, hence the overflow. The same occurs if you simply do
dim i as byte
i = -1
or even with
For i = 200 To 255
Debug.Print i
Next
as, at the end of the last loop, i is incremented in the next statement before doing the comparison (<=255) and this incrementation leads to an overflow error.

How to output number of occurrences of a positive negative number and Zero in Console Application?

I have a doubt on how to do this:
The user has to input 10 numbers, and the program has to output how many numbers are Positive, how many numbers are Negative, and how many are Zero, out of those 10 numbers. I remember getting it right once, but forgot the logic and so the code too. I tried this, but it doesn't seem to work. Can anyone hlep me out by giving me the solution? Thanks!
Code:
Dim counter, pluscount, minuscount, zerocount As Decimal
For counter = 1 To 10
Console.WriteLine("Enter 10 numbers")
counter = Console.ReadLine
If counter > 0 Then
pluscount = pluscount + 1
ElseIf counter < 0 Then
minuscount = minuscount + 1
Else
zerocount = zerocount + 1
End If
counter = counter + 1
Console.WriteLine(pluscount & " number/s is/are positive.")
Console.WriteLine(minuscount & " number/s is/are negative.")
Console.WriteLine(zerocount & " number/s is/are zero")
Next
This code simply implements the solution submitted by #jmcilhinney with the addition of a ReadLine and a CDec. The correct answer is #jmcilhinney's
Module Module1
Sub Main()
'Add a variable to hold user input
Dim counter, input, pluscount, minuscount, zerocount As Decimal
For counter = 1 To 10
Console.WriteLine("Enter a number")
'do not change the value of counter in the loop
'use a different variable to hold user input
'Turn on Option Strict
input = CDec(Console.ReadLine)
If input > 0 Then
pluscount = pluscount + 1
ElseIf input < 0 Then
minuscount = minuscount + 1
Else
zerocount = zerocount + 1
End If
'Do not increment the counter
'Next increments the counter automaticly
'counter = counter + 1
Next
Console.WriteLine(pluscount & " number/s is/are positive.")
Console.WriteLine(minuscount & " number/s is/are negative.")
Console.WriteLine(zerocount & " number/s is/are zero")
'Stop the program from closing with a .ReadLine so you can see your results
Console.ReadLine()
End Sub
End Module
The issue is how you're using counter. It is the loop counter and thus should be nothing else. Don't assign anything to it in the loop and don't increment it manually. When you call ReadLine, you need some other variable to assign the result to.
Also, there's no array there. Your loop counter should go from 0 to 9 and that should be the index into the array when you set an element.
Finally, don't tell the user to enter 10 numbers inside the loop. If you do that, you'll be teeming then to enter 10 numbers 10 times.
You Need To Findout the Count Right...Then You Have to Mention that Console.Writeline after Completing the For Loop Like This..
Dim counter, pluscount, minuscount, zerocount As Decimal
For counter = 1 To 10
Console.WriteLine("Enter 10 numbers")
counter = Console.ReadLine
If counter > 0 Then
pluscount = pluscount + 1
ElseIf counter < 0 Then
minuscount = minuscount + 1
Else
zerocount = zerocount + 1
End If
counter = counter + 1
Next
Console.WriteLine(pluscount & " number/s is/are positive.")
Console.WriteLine(minuscount & " number/s is/are negative.")
Console.WriteLine(zerocount & " number/s is/are zero")
First you need to take user input in an array. This array will have 10 elements which will be input by the user.
After the user has finished the input, you loop over the array and find which of the numbers entered are positive, negative or zero.
So in short, you need one array with 10 elements, 1 counter for the loop and 3 variables to hold the number of positive, negative or zero numbers entered.

vba excel If condition error on final iteration

I'm having the code takes the input from my checkboxes and grab data from the related worksheet. I ran it line by line and found out that it always gets a runtime error at the If statement on the final loop. Is there something wrong in my code?
Option Explicit
Private Sub UserForm_Initialize()
Dim counter As Long
Dim chkBox As MSForms.CheckBox
''''Add checkboxes based on total sheet count
For counter = 1 To Sheets.count - 2
Set chkBox = Me.Frame1.Controls.Add("Forms.CheckBox.1", "CheckBox" & counter)
chkBox.Caption = Sheets(counter + 2).Name
chkBox.Left = 10
chkBox.Top = 5 + ((counter - 1) * 20)
Next
End Sub
Private Sub cmdContinue_Click()
Dim Series As Object
Dim counter As Long
'''Clear old series
For Each Series In Sheets(2).SeriesCollection
Sheets(2).SeriesCollection(1).Delete
Next
''Cycle through checkboxes
For counter = 1 To Sheets.count - 2
''If the box is checked then
If Me.Frame1.Controls(counter).Value = True Then ''Error here on 4th iteration
''Add new series
With Sheets(2).SeriesCollection.NewSeries
.Name = Sheets(counter + 2).Range("$A$1")
.XValues = Sheets(counter + 2).Range("$A$12:$A$25")
.Values = Sheets(counter + 2).Range("$B$12:$B$25")
End With
End If
Next counter
Me.Hide
End Sub
Also, a second problem is it always run on the wrong loop. If i check box 2 it'll run data for the box 1 sheet, 3 run for 2, 4 run for 3, and 1 run for 4. Can anyone explain the reason behind this?
EDIT: So as VincentG point out below, adding an explicit name "checkbox" in there did the trick (i didn't know you could do that). Index 1 was probably taken by one of the buttons or the frame in the user form, causing it to get off set.
I guess your main problem comes from the fact that the controls have to be accessed starting from index 0. So to loop over all controls, you would do something like
For counter = 0 To Me.Frame1.Controls.Count - 1
Debug.Print counter; Me.Frame1.Controls(counter).Name
Next counter
So, when you stick to you code, I assume you have to change the if-statement to
If Me.Frame1.Controls(counter-1).Value = True Then

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

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.

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).