SIlly fun question?

countryfan_nt

Well-known Member
Joined
May 19, 2004
Messages
765
Office Version
  1. 365
Platform
  1. Windows
Hello Friends,

Here comes a crazy fun question.

I was wondering is there a macro where when it is run you would see numbers popping up one after another in cell B5. Numbers start from 100 to 0.

Thanks and All the best,
Nawaf
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Code:
Sub Numbers()
Dim i As Integer, waitTime As Variant

For i = 0 To 100
    Range("B5").Value = i
    waitTime = TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1)
Application.Wait waitTime

Next i

MsgBox "Finished!"

End Sub
 
Upvote 0
Nice code but........

It is running now but unless I want to kill xl I have to wait 100 seconds for it to stop!! :-(
 
Upvote 0
Yes, I know. I don't know any way around that off of the top of my head, though.

If you run that code without the Wait statement, it goes too fast to watch the numbers change and all you really see is the final result of 100.
 
Upvote 0
Here are a couple ideas you can try.
(1) Using Krysty's Wait method...
Code:
Sub Numbers()
Dim i As Integer, waitTime As Variant
On Error GoTo Quit
For i = 0 To 100
If [B4] <> "" Then Exit For
    Range("B5").Value = i
    waitTime = TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 1)
    Application.Wait waitTime
    Next i
MsgBox "Finished!"
Quit:
Range("B5").Value = "Stopped"
End Sub
You can simply hit the ESC button to stop the code.

(2) With these 3 routines (and a second button for the "StopIt" routine) you can start the Numbers2 routine and then hit the button for the "StopIt" routine to make it stop.
Code:
Sub Numbers2()
If [B4] = "" Then
    If [B5] < 100 Then
        Application.OnTime Now + TimeSerial(0, 0, 1), "IncrementB5"
      Else: MsgBox "Finished"
    End If
End If
End Sub


Sub IncrementB5()
Range("B5").Value = Range("B5").Value + 1
Numbers2
End Sub


Sub StopIt()
[B4] = "Stop"
End Sub
The OnTime method will allow user interaction where as the Wait command won't.

Hope it helps.
Dan
 
Upvote 0

Forum statistics

Threads
1,226,618
Messages
6,192,052
Members
453,693
Latest member
maverick688

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