Application.OnTime

Biz

Well-known Member
Joined
May 18, 2009
Messages
1,773
Office Version
  1. 2021
Platform
  1. Windows
Dear All,

I'm trying change colour every sec in worksheet.

This Workbook module
Code:
Private Sub Workbook_Open()
'MsgBox "Bank_format_print " & Now
Call RunEverySec
End Sub

Standard Module
Code:
Public nTime As Double
Sub RunEverySec()
nTime = Now + TimeValue("00:00:01")
Application.OnTime nTime, "Colors56"
Calculate
End Sub
 
Sub colors56()
  'Application.Calculation = xlCalculationManual
Dim i As Long

  Cells(1, 1).Interior.ColorIndex = Application.RandBetween(0, 56)
done:
  'Application.Calculation = xlCalculationAutomatic
End Sub

Code fails at Application.OnTime nTime, "Colors56"

Any help would be greatly appreciated.

Biz
 

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

Code:
Public nTime As Date
Sub RunEverySec()
Call colors56
Calculate
End Sub
 
Sub colors56()
  'Application.Calculation = xlCalculationManual
Dim i As Long

  Cells(1, 1).Interior.ColorIndex = Evaluate("RandBetween(0, 56)")
done:
  'Application.Calculation = xlCalculationAutomatic
nTime = Now + TimeValue("00:00:01")
Application.OnTime nTime, "Colors56"

End Sub
 
Upvote 0
Hi Vog,

Thanks for Vog code works.

Only problem I have now if I exit saving the macro. It starts the macro automatically again.

What vba code do I use to fix this problem?

Kind Regards,

Biz
 
Upvote 0
Try

Code:
Sub StopIt()
Application.OnTime nTime, "Colors56", , False
End Sub
 
Upvote 0
Hi Vog,

How about using Sub Workbook_BeforeSave?

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI = True Then
    Call StopIt
    ActiveWorkbook.Close True ' closes the active workbook and saves any changes
Else: If Cancel = True Then ActiveWorkbook.Close False ' closes the active workbook without saving any changes
End If
End Sub

The above macro is failing.

Biz
 
Upvote 0
Hi Vog,

Macro is failing because when exiting the file:
* When "Yes" clicked the macro not only saves file but restarts the macro again. It should save the file and then exit file only.
* when "No" clicked the macro quits the files and restarts it again. It should not save the file and then exit file only.

Is there a way to fix this problem?

Biz
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,903
Members
452,948
Latest member
Dupuhini

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