Overflow error, but I can't get it to repeat it

mcomp72

Board Regular
Joined
Aug 14, 2016
Messages
155
A couple of months ago, I found some code online to create a delay when running VBA code, called SleepSub. I needed to use it because on Excel for Mac, some labels on my UserForms were not updating on the screen, even when I would use the Repaint method. (I think the labels were changing too fast for Excel to properly update them, though I never had that problem on Windows.) I found that if I called SleepSub on a Mac, the labels WOULD be updated on the screen.

On Excel 2011 for Mac, that sub just generated an overflow error. I have run this sub many times before without that error. What's worse is, I can't get it to repeat the error. Here's the full code of the sub:

Code:
Sub SleepSub(vSeconds As Variant)
'this sub will delay the code running for however many seconds are in vSeconds.

Dim t0 As Single, t1 As Single

t0 = Timer

Do
    t1 = Timer
    If t1 < t0 Then t1 = t1 + 86400 'Timer overflows at midnight
    DoEvents    'optional, to avoid excel freeze while sleeping
Loop Until t1 - t0 >= vSeconds
    
End Sub
The sub is inside a standard module. I call the sub from within a UF module, like so:

Code:
Sub UpdateStatusLabel(PercentFinished As Integer)

Me.StatusLabel.Caption = PercentFinished & "% complete"
Me.Repaint
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  Mac Then
    SleepSub(0.05)
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

End Sub
Anyone have any idea why I would sometimes receive an overflow error in SleepSub?
 

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,283
did you try using double instead of single.
Can't really get my head around the issue completely, just this is the first thing that comes to mind.
 

mcomp72

Board Regular
Joined
Aug 14, 2016
Messages
155
I haven't tried that yet, but that's a good idea. I will make that change.

If anyone thinks of any other reason why it might be causing that error, I'm all ears. I wish there was a way to reproduce the error, but so far, it hasn't happened again, even with the variable types being Single.
 

Forum statistics

Threads
1,086,235
Messages
5,388,624
Members
402,127
Latest member
Jemx

Some videos you may like

This Week's Hot Topics

Top