PDA

View Full Version : Noob question about Do-Until Loops



ericdbyrd
01-14-2023, 12:32 AM
So I'm having a problem with a Do-Until Loop.


As you know, when the test condition goes True, execution jumps out of the Loop, instead of continuing to loop around inside.


I had a situation in which the test condition went True, but execution continued to loop around inside, instead of jumping out. Of course this clustered up my entire results.


The test condition compared two variables, both declared as Double. They were time values, and I always declare time variables as Double, to help prevent rounding errors.


So when the two time values were equal, the test condition should have gone True, and the Loop should have ended. The two variables both had a value of exactly .54375, which is equivalent to 1:03:00 pm. Because there are only 5 digits in the mantissa, it's hard to imagine this was caused by a rounding error... my best guess is, it's some kind of Excel checksum thing.


Ultimately I used the Editor to put execution on the next statement, and finished the run that way.


This program has been run hundreds of times, and this is the first time this has happened. I've never before seen a Test condition go True in a Do-Until Loop, without execution jumping out like it should.

SamT
01-14-2023, 02:13 AM
5 digits is what you saw, but Excel Stores Times as 15 digits

You're best to just format them as Dates ("hh:mm:ss") OR, if they are always full minutes, Format as "hh:mm"


Dim T1 as String, T2 As String
Dim Time1 As Date, Time 2 As Date

T1 = Format(Time1, "hh:mm:ss")
'Alternate Format
T2 = Format(Time2 , "hh:mm")
Do
'
'
Until T1 = T2

ericdbyrd
01-14-2023, 10:00 PM
Thanks SamT. You're right, I was not aware of that fact.

I have started re-declaring all the time variables as Date, not as Double, and also declared two dummy variables as String. In every place where I compare time values in a test condition, I will write in statements equating the dummy variable as Format(hh:mm:ss) of the Date variables; and then compare the dummies in the test condition. It'll be extra work, but if it prevents this kind of glitch when this is running in real time, it will be worth it.

I will pay this forward, since it's not likely I'll be able to return the favor.