Byte in Backward Loop

DocAElstein

Banned user
Joined
May 24, 2014
Messages
1,336
Hi,
. Sorry- probably an obvious answer. But Googling gave mostly only guesses.
. I prefer sometimes to limit things initially in size and so often dimension variables as Byte ( 0 - 255 ).
. I noticed something Weird.
. This works:
Code:
Sub TestByteloopForwards()
Dim Count As Byte
  For Count = 1 To 2 Step 1
  MsgBox "Hi Weld"
  Next Count
End Sub
. This does not:
Code:
Sub TestByteloopBackwards()
Dim Count As Byte
  For Count = 2 To 1 Step -1
  MsgBox "Hi Weld"
  Next Count
End Sub

. Obviously I can change to an Integer or even something bigger.
.. But I am learning, and would be interested to find out why
. Any ideas?

Thanks
Alan
 
Only if you are really really bored and feel like it... I am not in the least grasping why it would need to be a Double to work in a loop.

Hi Mark,

It doesn't really need to be a double, but it must be coercible to a Double - which is really just another way of saying it must be capable of being interpreted as a number (following the Let Coercion rules in the spec). Hence you can pass things like "1.2" as a string and the loop will still work. Of course, I don't know why you would do that but at least VBA will forgive you if you do. ;)
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Ahh, something my wee brain can grasp ........

So..
… The 3 MrExcel Profi’s have taken us mortals to a level of understanding in the For loop where no googling or written specification has been able to take us before!
.. A last summary for my wee inexperienced brain.
.. The TYPE of Start, Stop or Step variable must be capable of being seen to be of the type of the “bound” variable (my original “Count”). So it is perfectly alright to (as I do) pick Integer or a even Byte for the variables, (but noting that a –ve variable start stop or step will fail if the “bound” variable (my original “Count”) is defined as a Byte, as the Byte has no representation of the - ).
.. Additionally the start stop or step will then be converted to a (Long) Number.
. Thanks again guys!
Alan

P.s. 1.
….Hence you can pass things like "1.2" as a string and the loop will still work. Of course, I don't know why you would do that but ….
- I think I can sort of see a good reason why it is useful for the loop to tolerate such values. . If rather than actual numbers you are using variables for these numbers and these variables are the result of a calculation which may introduce some rounding errors, then the loop will tolerate that.

P.s. 2. I further experimented whereby I defined all my variables as long except for the “bound” variable (my original “Count”), which I defined as Byte. Never the less a –ve step failed. Clearly the For loop overwrote my declaration, confirming that all variables in the For loop are made to be seen as that of the “bound” variable (my original “Count”)
 
Upvote 0
Additionally the start stop or step will then be converted to a (Long) Number.
That's one bridge too far:

Code:
Sub DAE()
    Dim d As Double

    For d = Exp(1) To WorksheetFunction.Pi() ^ 2 Step (1 + Sqr(5)) / 2
        Debug.Print d
    Next d
End Sub

There are numerical reasons not to use a floating point number as the loop variable, but not a language reason.
 
Upvote 0
That's one bridge too far:

Code:
Sub DAE()
    Dim d As Double

    For d = Exp(1) To WorksheetFunction.Pi() ^ 2 Step (1 + Sqr(5)) / 2
        Debug.Print d
    Next d
End Sub

There are numerical reasons not to use a floating point number as the loop variable, but not a language reason.

Hi
. The Mortal brain does not get it again, sorry.

…….

Code:
Sub DocAElsteinLong()
    Dim d As Long
    Let Stepvalue = (1 + Sqr(5)) / 2 '1.618
    Let StopValue = WorksheetFunction.Pi() ^ 2 '9.869
    Let Startvalue = Exp(1) '2.718
    For d = Exp(1) To WorksheetFunction.Pi() ^ 2 Step (1 + Sqr(5)) / 2
        Debug.Print d
    Next d
    For d = Startvalue To StopValue Step Stepvalue
        Debug.Print d
    Next d
    Let Stepvalue = 1.618 '(1 + Sqr(5)) / 2
    Let StopValue = 9.869 ' WorksheetFunction.Pi() ^ 2
    Let Startvalue = 2.718 'Exp(1)
    For d = Startvalue To StopValue Step Stepvalue
    Debug.Print d
    Next d
End Sub

Code:
Sub DocAElsteinDouble()
    Dim d As Double
    Let Stepvalue = (1 + Sqr(5)) / 2 '1.618
    Let StopValue = WorksheetFunction.Pi() ^ 2 '9.869
    Let Startvalue = Exp(1) '2.718
    For d = Exp(1) To WorksheetFunction.Pi() ^ 2 Step (1 + Sqr(5)) / 2
        Debug.Print d
    Next d
    For d = Startvalue To StopValue Step Stepvalue
        Debug.Print d
    Next d
    Let Stepvalue = 1.618 '(1 + Sqr(5)) / 2
    Let StopValue = 9.869 ' WorksheetFunction.Pi() ^ 2
    Let Startvalue = 2.718 'Exp(1)
    For d = Startvalue To StopValue Step Stepvalue
    Debug.Print d
    Next d
End Sub

. Wot does this mean.
. The variables are not converted to long?. They are converted to the type of the bound variable only
 
Upvote 0


..

Thanks…

Then wot did this mean…??

…….

I think there's a two stage process in that all values are coerced to the declared variable type first, then to Double, which explains the infinite loop if you run this:
Code:
Sub foobar()
    Dim n As Long
    For n = 0.6 To 1.2 Step 0.1
        Debug.Print n
        DoEvents
    Next n
End Sub
because the step value is actually 0.



… This „works“::

Code:
Sub foolbar()
    Dim n As Double
    For n = 0.6 To 1.2 Step 0.1
        Debug.Print n
        DoEvents
    Next n
End Sub

Thanks for your patients !
Alan Elston
 
Upvote 0
A Long only represents whole numbers. We've kind of reached the end of the rope until you understand how computers store numbers, Doc.
 
Upvote 0
A Long only represents whole numbers. We've kind of reached the end of the rope until you understand how computers store numbers, Doc.

Ok, Thanks, was just the two stage bit that was still confusing me.
. I just see the one.. that of converting type to that of the bound variable.
I guess the penny will drop

Thanks again
 
Upvote 0
A simplifying approach for now would be to always use Longs for loop index variables, and not worry about the minutiae. That will get you a long way downstream.
 
Upvote 0

Forum statistics

Threads
1,223,749
Messages
6,174,288
Members
452,554
Latest member
Louis1225

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top