UltimateTimer - C# to VB.net Conversion & Start/End Syntax Error? - vb.net

Trying out Tim Lovell-Smiths' UltimateTimer project.
http://blogs.msdn.com/b/tilovell/archive/2014/01/29/a-light-weight-net-threadpool-timer-class.aspx
http://blogs.msdn.com/b/tilovell/archive/2014/01/31/sample-using-ultimatetimer-threadpooltimer.aspx
Trying to convert his C# to VB.net but his function(lambda??) in Sub Main Line 2 is not correct translation / syntax ?? - "EXPRESSION DOES NOT PRODUCE A VALUE"
1) How do I fix that Function Error in Line Two of Sub Main in VB.net
2) Is My Code properly set up to start the Timer from my SheetBeforeDoubleClick Event ?
3) Is My Code properly set up to end the Timer from my SheetBeforeDoubleClick Event ?
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Core
Imports ExcelDna.Integration
Imports System.Threading
Imports UltimateTimer
Public Class AddIn
WithEvents Application As Application
Shared timer As ThreadPoolTimer
Private Shared Sub Main(ByVal args() As String)
'ERROR NEXT LINE "EXPRESSION DOES NOT PRODUCE A VALUE" - OnTimer(timer)
timer = ThreadPoolTimer.Create(Function() OnTimer(timer))
timer.SetTimer(DateTime.Now.AddSeconds(3), msPeriod:=0, acceptableMsDelay:=0)
Console.WriteLine("Press any key to stop timer")
Console.ReadLine()
timer.Dispose()
End Sub
Private Shared Sub OnTimer(timer As ThreadPoolTimer)
Console.WriteLine("Timer was called back! Resetting timer. The time is now " & DateTime.Now.ToString())
timer.SetTimer(DateTime.Now.AddSeconds(3), 0, 0)
End Sub
Private Shared Sub Endtimer(timer As ThreadPoolTimer)
timer.Dispose()
End Sub
Private Sub Application_SheetBeforeDoubleClick(Sh As Object, Target As Range, ByRef Cancel As Boolean) Handles Application.SheetBeforeDoubleClick
If Target.Address = "$A$1" Then
OnTimer(timer)
MsgBox("TIME Is On My Side!!", , "Yeppers")
ElseIf Target.Address = "$Z$1" Then
Endtimer(timer)
MsgBox("I Stopped TIME !!", , "Oh-Ho")
End If
End Sub
End Class
Thanks… :o)
EDIT: Oct 12, 2014
Okay with Dave D's help I got Tim's UltimateTimer working for me in vb.net and as part of my Excel ExcelDNA xLL AddIn.
Here is the Imports, code and sample Framework (and other code i am using for my xLL AddIN tests) for this timer to work inside a packed xLL for Excel use.
I am calling it from a double click but it could be called from a right click menu... etc.
Public Module MyFunctions
<ExcelFunction(Description:="My first .NET function")> _
Public Function dnaHello(name As String) As String
Return "Hello " & name
End Function
End Module
Public Class AddIn
Implements IExcelAddIn
WithEvents Application As Application
WithEvents Button As CommandBarButton
Public Sub AutoOpen() Implements IExcelAddIn.AutoOpen
Application = ExcelDnaUtil.Application
' Add Cell context menu
Dim ContextMenu As CommandBar
ContextMenu = Application.CommandBars("Cell")
Button = ContextMenu.Controls.Add(Type:=MsoControlType.msoControlButton, Before:=ContextMenu.Controls.Count, Temporary:=True)
With Button
.Caption = "Excel-DNA Test Button"
.Tag = "EXCEL-DNA-Test"
End With
End Sub
Public Sub AutoClose() Implements IExcelAddIn.AutoClose
Button.Delete()
End Sub
Shared timer As ThreadPoolTimer
Private Shared Sub TimerMain()
timer = ThreadPoolTimer.Create(Sub() OnTimer(timer))
timer.SetTimer(DateTime.Now.AddSeconds(3), msPeriod:=0, acceptableMsDelay:=0)
End Sub
Private Shared Sub OnTimer(timer As ThreadPoolTimer)
MsgBox("TIME Is On My Side!!" & DateTime.Now.ToString(), , "Yeppers")
timer.SetTimer(DateTime.Now.AddSeconds(3), 0, 0)
End Sub
Private Shared Sub Endtimer(timer As ThreadPoolTimer)
timer.Dispose()
End Sub
Private Sub Application_SheetBeforeDoubleClick(Sh As Object, Target As Range, ByRef Cancel As Boolean) Handles Application.SheetBeforeDoubleClick
If Target.Address = "$A$1" Then
MsgBox("TIME Is On My Side!!", , "Yeppers")
TimerMain()
ElseIf Target.Address = "$E$1" Then
Endtimer(timer)
MsgBox("I Stopped TIME !", , "Oh-Ho")
End If
End Sub
Private Sub Button_Click(Ctrl As CommandBarButton, ByRef CancelDefault As Boolean) Handles Button.Click
Application.StatusBar = "Excel-DNA Test Button - Clicked!"
End Sub
End Class

Related

Certain events not firing for combobox

I have a userform with programmatically created comboboxes, which I need to run events on. As per the advise here, I created a wrapper class which I put around each such combobox ("event listener").
This is the rough content of the clsEvntListnr class module
Public WithEvents cb As MSForms.ComboBox
Public frm As UserForm
Private Sub cb_Change()
CollectGarbage
End Sub
Private Sub cb_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'stuff
End Sub
Private Sub cb_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'stuff
End Sub
Private Sub cb_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'stuff
End Sub
Private Sub cb_DropButtonClick()
'stuff
End Sub
Private Sub cb_Enter()
'stuff
End Sub
Private Sub cb_Exit()
'stuff
End Sub
Private Sub cb_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'stuff
End Sub
Private Sub cb_Click()
'stuff
End Sub
Private Sub cb_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'stuff
End Sub
Private Sub cb_AfterUpdate()
'stuff
End Sub
This is how the comboboxes are created (as a part of an event for another combobox). The C_COMBOS at the end is a globally declared collection.
Private Sub cbTransaction_Change()
Dim oEvtListnr As clsEventListener
'other stuff and declarations
For i = LBound(var) To UBound(var)
Set ctrl = Me.Controls.Add("forms.combobox.1", "ctrlTb" & i, True)
Set oEvtListnr = New clsEventListener
Set oEvtListnr.cb = ctrl
Set oEvtListnr.frm = Me
C_COMBOS.Add oEvtListnr
next i
End sub
Now the behaviour is mostly as expected with the exceptions that certain event just will not fire. From the events I defined in the class module, the following do fire:
cb_KeyDown, cb_KeyPress, cb_KeyUp, cb_DropButtonClick, cb_DblClick, cb_MouseUp
while these do not:
cb_Change, cb_Click, cb_Enter, cb_Exit, cb_AfterUpdate
I have made the obvious tests by putting in breaks into these events and indeed they simply do not fire up. Any idea what may be the issue?

VBA raising events from modeless Userfrom

I'm trying to raise events from a modeless userform. My starting point is this excellent example. When I show the form modeless, the code raising the event executes, but the event handler never runs (I don't get the expected MsgBox when the Cancel button is clicked.) When I show the form modal, the events are handled as desired, but the form is no longer modeless as desired.
The userform named FormWithEvents has an OKButton and a CancelButton; here's the code behind:
Option Explicit
Public Event FormConfirmed()
Public Event FormCancelled(ByRef Cancel As Boolean)
Private Function OnCancel() As Boolean
Dim cancelCancellation As Boolean
RaiseEvent FormCancelled(cancelCancellation)
If Not cancelCancellation Then Me.Hide
OnCancel = cancelCancellation
End Function
Private Sub CancelButton_Click()
OnCancel
End Sub
Private Sub OKButton_Click()
Me.Hide
RaiseEvent FormConfirmed
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = Not OnCancel
End If
End Sub
Here's the code for the Presenter class that shows the form:
Option Explicit
Private WithEvents myModelessForm As FormWithEvents
Public Sub Show()
Set myModelessForm = New FormWithEvents
' COMMENT OUT ONE OF THE FOLLOWING TWO LINES TO TOGGLE MODELESS OR MODAL
myModelessForm.Show vbModeless ' Modeless, but events don't get handled (no msgbox on clicking cancel button)
' myModelessForm.Show vbModal ' Events get handled, but no longer modal
End Sub
Private Sub myModelessForm_FormCancelled(Cancel As Boolean)
' Setting cancel to True will leave the form open
Cancel = MsgBox("Cancel this operation?", vbYesNo + vbExclamation) = vbNo
If Not Cancel Then
' Modeless form was cancelled and is now hidden
' ...
Set myModelessForm = Nothing
End If
End Sub
Private Sub myModelessForm_FormConfirmed()
' Form was okayed and is now hidden
Set myModelessForm = Nothing
End Sub
And here's the code in the main module:
Option Explicit
Public Sub RunForm()
With New Presenter
.Show
End With
End Sub
Any ideas on where I've gone wrong?
Maybe this instead so you can keep your Presenter instance in scope:
Dim pres as Presenter
Public Sub RunForm()
Set pres = New Presenter
pres.Show
End Sub
Avoid using RaiseEvent. Declare a public instance of the Presenter class in the standard module.
Option Explicit
Public Preter As New Presenter
Public Sub RunForm()
With New Presenter
.Show
End With
End Sub
Convert the event procedure to a sub with public scope.
Sub FormCancelled(Cancel As Boolean)
' Setting cancel to True will leave the form open
Cancel = MsgBox("Cancel this operation?", vbYesNo + vbExclamation) = vbNo
If Not Cancel Then
' Modeless form was cancelled and is now hidden
' ...
Set myModelessForm = Nothing
End If
End Sub
The call the sub within the class.
Private Function OnCancel() As Boolean
Dim cancelCancellation As Boolean
Preter.FormCancelled cancelCancellation
If Not cancelCancellation Then Me.Hide
OnCancel = cancelCancellation
End Function
I'm not sure but your code line Set myModelessForm = Nothing in the class module bother me. I wonder whether the form should be hidden instead so that the code can continue to the Show procedure where the form can then be put to rest.

An instance of an Excel-VBA form opens with an error, if it was closed from the top right red `X`

Prehistory
I have read the best practises for creating a form, concerning the fact that one should always refer to an object of the form and not the form itself. Thus, I have decided to build a boiler-plate form for myself.
The problem
Everything ran smoothly, until the moment I have decided to close the form with the top right red X. It closes ok. But then, when I try to open the form again, I get this runtime error:
The error is on objPresenter.Show (see the code below). Obviously, it does not enter in the if above. But the problem is that the closing from the X does not work fine. When I close the form from the End button, anything works. And even, if I copy the code for the closing from the btnEnd to UserForm_QueryClose it still does not work the same.
The form
Thus, I have a modMain, frmMain and clsSummaryPresenter, which all take care of the form. I start the code from modMain
My form looks like this:
It has btnRun, btnExit, lblInfo. The name of the class is frmMain.
The code
In frmMain:
Option Explicit
Public Event OnRunReport()
Public Event OnExit()
Public Property Get InformationText() As String
InformationText = lblInfo.Caption
End Property
Public Property Let InformationText(ByVal value As String)
lblInfo.Caption = value
End Property
Public Property Get InformationCaption() As String
InformationCaption = Caption
End Property
Public Property Let InformationCaption(ByVal value As String)
Caption = value
End Property
Private Sub btnRun_Click()
RaiseEvent OnRunReport
End Sub
Private Sub btnExit_Click()
RaiseEvent OnExit
End Sub
Private Sub UserForm_QueryClose(CloseMode As Integer, Cancel As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
Hide
'Even if I change the two lines above with this the error happens:
'RaiseEvent OnExit
'However, if I simply write END in stead of those two lines
'anything works quite ok...
'but that is a bit brutal.
End If
End Sub
In clsSummaryPresenter
Option Explicit
Private WithEvents objSummaryForm As frmMain
Private Sub Class_Initialize()
Set objSummaryForm = New frmMain
End Sub
Private Sub Class_Terminate()
Set objSummaryForm = Nothing
End Sub
Public Sub Show()
If Not objSummaryForm.Visible Then
objSummaryForm.Show vbModeless
Call ChangeLabelAndCaption("Press Run to Start", "Starting")
End If
With objSummaryForm
.Top = CLng((Application.Height / 2 + Application.Top) - .Height / 2)
.Left = CLng((Application.Width / 2 + Application.Left) - .Width / 2)
End With
End Sub
Public Sub Hide()
If objSummaryForm.Visible Then objSummaryForm.Hide
End Sub
Public Sub ChangeLabelAndCaption(strLabelInfo As String, strCaption As String)
objSummaryForm.InformationText = strLabelInfo
objSummaryForm.InformationCaption = strCaption
objSummaryForm.Repaint
End Sub
Private Sub objSummaryForm_OnRunReport()
MainGenerateReport
Refresh
End Sub
Private Sub objSummaryForm_OnExit()
Hide
End Sub
Public Sub Refresh()
With objSummaryForm
.lblInfo = "Ready"
.Caption = "Task performed"
End With
End Sub
In modMain
Option Explicit
Private objPresenter As clsSummaryPresenter
Public Sub MainGenerateReport()
objPresenter.ChangeLabelAndCaption "Starting and running...", "Running..."
GenerateNumbers
End Sub
Public Sub GenerateNumbers()
Dim lngLong As Long
Dim lngLong2 As Long
tblMain.Cells.Clear
For lngLong = 1 To 4
For lngLong2 = 1 To 1
tblMain.Cells(lngLong, lngLong2) = lngLong * lngLong2
Next lngLong2
Next lngLong
End Sub
Public Sub ShowMainForm()
If (objPresenter Is Nothing) Then
Set objPresenter = New clsSummaryPresenter
End If
objPresenter.Show
End Sub
The question
Once again, why I cannot close the form with the red X? I can substitute the code in UserForm_QueryClose with End but that is a bit brutal. Any ideas?
Changing the form's mode from vbModeless to vbModal gives you an earlier and more informative failure:
The problem seems to be because the Cancel = True assignment in the QueryClose handler, isn't working for some reason.
The signature for the QueryClose handler is as follows:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Yours is:
Private Sub UserForm_QueryClose(CloseMode As Integer, Cancel As Integer)
You should never type these handler signatures manually yourself - instead, use the drop-down in the codepane's top-right corner, and have the VBE generate the handler stubs for you:
That way your handler signatures will always match the interface they're for.
VBA doesn't really care about parameter names in handlers: the way the runtime matches a handler signature is by matching the parameter indices and their types, against the expected ones. Since both QueryClose parameters are Integer values, inverting them compiles just fine - except when you set Cancel = True, what the runtime sees is that you've assigned CloseMode = -1 and left the Cancel parameter alone.
Which means your form doesn't cancel its close, and thus the object gets destroyed every time.
Invert the parameters in your QueryClose handler, and everything works perfectly fine and exactly as intended.
Calling the form like so works just fine for me:
Option Explicit
dim mfrmMain as ufMain
Sub ShowMainForm2()
If ufMain Is Nothing Then
Set ufMain = New mfrmMain
End If
mfrmMain.Show vbModeless
End Sub

Cannot Run Macro Created with Add-in in Excel

I have create an xla (excel add-in) that have a function to protect the document (so that user could protect the document without knowing the password). This xla is added in every excel file that need this functionality.
when the xla is installed or added to excel, the protect button will be added in last menu. but when i click the button, an error occur show that
"Cannot run the macro Pivot Add-In 0.2'!protectSheet'". The macro may not be available in this workbook or all macros may be disabled."
The code that event handler onclicked is protectSheet, please see the source below:
Could anyone pointed my why this problem occur?
on ThisWorkbook
'main function'
Public Sub protectSheet()
ActiveWorkbook.ActiveSheet.protect Password:="password", AllowUsingPivotTables:=True
End Sub
Public Sub ShowToolbar()
' Assumes toolbar not already loaded '
Application.CommandBars.Add Module1.TOOLBARNAME
AddButton "Proteksi Sheet", "Memproteksi Pivot", 3817, "protectSheet"
' call AddButton more times for more buttons '
With Application.CommandBars(Module1.TOOLBARNAME)
.Visible = True
.Position = msoBarTop
End With
End Sub
Public Sub AddButton(caption As String, tooltip As String, faceId As Long, methodName As String)
Dim Btn As CommandBarButton
Set Btn = Application.CommandBars(Module1.TOOLBARNAME).Controls.Add
With Btn
.Style = msoButtonIcon
.faceId = faceId ' choose from a world of possible images in Excel: see http://www.ozgrid.com/forum/showthread.php?t=39992 '
.OnAction = methodName
.TooltipText = tooltip
End With
End Sub
Public Sub DeleteCommandBar()
Application.CommandBars(TOOLBARNAME).Delete
End Sub
'called when add-in installed
Private Sub Workbook_AddinInstall()
Call ShowToolbar
End Sub
'called when add-in uninstalled
Private Sub Workbook_AddinUninstall()
Call DeleteCommandBar
End Sub
On module1
Public Const TOOLBARNAME = "PivotTools"
After moving all function to module1 , then retain caller function on ThisWorkbook the error now gone. Seem that i have to define all functionality that call/ use constant (Public Const TOOLBARNAME = "PivotTools") in the same file (in my case in module1)
on module1 file
Public Const TOOLBARNAME = "PivotTools"
'caller method'
Public Sub protectDoc()
On Error GoTo errorInfo
protectSheet
'if success, show msg box'
MsgBox ("Report berhasil diproteksi")
Exit Sub
errorInfo:
MsgBox Err.Description & vbCrLf & Err.Number
End Sub
Public Sub protectSheet()
ActiveWorkbook.ActiveSheet.protect Password:="password", AllowUsingPivotTables:=True
End Sub
Public Sub refreshDoc()
On Error GoTo errorInfo
refreshConnection
protectSheet
'if success, show msg box'
MsgBox ("Report berhasil diperbaharui")
Exit Sub
errorInfo:
MsgBox Err.Description & vbCrLf & Err.Number
End Sub
Private Sub refreshConnection()
ActiveWorkbook.ActiveSheet.Unprotect Password:="password"
ActiveWorkbook.RefreshAll
End Sub
Public Sub ShowToolbar()
' Assumes toolbar not already loaded '
Application.CommandBars.Add TOOLBARNAME
AddButton "Proteksi Sheet", "Memproteksi Pivot", 225, "protectDoc"
AddButton "Refresh Data", "Refresh Pivot", 459, "refreshDoc"
' call AddButton more times for more buttons '
With Application.CommandBars(TOOLBARNAME)
.Visible = True
.Position = msoBarTop
End With
End Sub
Public Sub AddButton(caption As String, tooltip As String, faceId As Long, methodName As String)
Dim Btn As CommandBarButton
Set Btn = Application.CommandBars(TOOLBARNAME).Controls.Add
With Btn
.Style = msoButtonIcon
.faceId = faceId
' choose from a world of possible images in Excel: see http://www.ozgrid.com/forum/showthread.php?t=39992 '
.OnAction = methodName
.TooltipText = tooltip
End With
End Sub
Public Sub DeleteCommandBar()
Application.CommandBars(TOOLBARNAME).Delete
End Sub
on ThisWorkbook
'called when add-in installed
Private Sub Workbook_AddinInstall()
Call Module1.ShowToolbar
End Sub
'called when add-in uninstalled
Private Sub Workbook_AddinUninstall()
Call Module1.DeleteCommandBar
End Sub
I had this issue, but found that I had my module named the same as my sub (e.g. module named as "InsertLineID" and the sub was "InsertLineID").
Changing the module name to "LineID" and leaving the sub as "InsertLineID" worked a treat for me!

Timer on user form in Excel VBA

I've got some old Excel VBA code where I want to run a task at regular intervals. If I were using VB6, I would have used a timer control.
I found the Application.OnTime() method, and it works well for code that's running in an Excel worksheet, but I can't make it work in a user form. The method never gets called.
How can I make Application.OnTime() call a method in a user form, or are there other ways to schedule code to run in VBA?
I found a workaround for this. If you write a method in a module that just calls a method in your user form, then you can schedule the module method using Application.OnTime().
Kind of a kludge, but it'll do unless somebody has a better suggestion.
Here's an example:
''//Here's the code that goes in the user form
Dim nextTriggerTime As Date
Private Sub UserForm_Initialize()
ScheduleNextTrigger
End Sub
Private Sub UserForm_Terminate()
Application.OnTime nextTriggerTime, "modUserformTimer.OnTimer", Schedule:=False
End Sub
Private Sub ScheduleNextTrigger()
nextTriggerTime = Now + TimeValue("00:00:01")
Application.OnTime nextTriggerTime, "modUserformTimer.OnTimer"
End Sub
Public Sub OnTimer()
''//... Trigger whatever task you want here
''//Then schedule it to run again
ScheduleNextTrigger
End Sub
''// Now the code in the modUserformTimer module
Public Sub OnTimer()
MyUserForm.OnTimer
End Sub
I needed a visible countdown timer that could stay on top of other windows and run smoothly whether making changes to the workbook, or minimizing the Excel window. So, I adapted the #don-kirkby's creative code above for my own purposes and figured I'd share the result.
                      
The code below requires creation of a module and a userform as noted in the comments, or you can download the .xlsm at the bottom of this answer.
I used the Windows Timer API for more accurate and smooth countdown (and also customizable down to ~100 millisecond timer resolution, depending on your processor. There's even a "tick tock" sound. ⏰
Insert a new module and save it as modUserFormTimer. Add two form control command buttons to the worksheet, labelled Start Timer and Stop Timer and assigned procedures btnStartTimer_Click and btnStopTimer_Click.
Option Explicit 'modUserFormTimer
Public Const showTimerForm = True 'timer runs with/without the userform showing
Public Const playTickSound = True 'tick tock (a WAV sounds could be embedded: `https:// goo.gl/ ReuUyd`)
Public Const timerDuration = "00:00:20" 'could also Insert>Object a WAV for tick or alarm
Public Const onTimerStart_MinimizeExcel = True 'minimize Excel? (countdown remains visible)
Public Const onTimerStart_MaximizeExcel = True 'maximize Excel when timer completes?
'timer could be on top of other applications; instructions here: `https:// goo.gl/ AgmWrM`
'safe for 32 or 64 bit Office:
Private Declare PtrSafe Function SetTimer Lib "User32" (ByVal hWnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As Long
Private Declare PtrSafe Function KillTimer Lib "User32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Public Declare PtrSafe Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
Public schedTime As Date 'this is the "major" timer set date
Private m_TimerID As Long
Public Sub OnTimerTask()
'the procedure that runs on completion of the "major timer" (timer won't reschedule)
Unload frmTimer
''''''''''''''''''''''''''''''
MsgBox "Do Something!" ' < < < < < Do Something Here
''''''''''''''''''''''''''''''
End Sub
Public Sub btnStartTimer_Click()
schedTime = Now() + TimeValue(timerDuration)
InitTimerForm
End Sub
Public Sub btnStopTimer_Click()
'clicking the 'x' on the userform also ends the timer (disable the close button to force continue)
schedTime = 0
frmTimer.UserForm_Terminate
End Sub
Public Sub InitTimerForm()
'run this procedure to start the timer
frmTimer.OnTimer
Load frmTimer
If showTimerForm Then
If onTimerStart_MinimizeExcel Then Application.WindowState = xlMinimized
frmTimer.Show 'timer will still work if userform is hidden (could add a "hide form" option)
End If
End Sub
Public Sub StartTimer(ByVal Duration As Long)
'Begin Millisecond Timer using Windows API (called by UserForm)
If m_TimerID = 0 Then
If Duration > 0 Then
m_TimerID = SetTimer(0, 0, Duration, AddressOf TimerEvent)
If m_TimerID = 0 Then
MsgBox "Timer initialization failed!", vbCritical, "Timer"
End If
Else
MsgBox "The duration must be greater than zero.", vbCritical, "Timer"
End If
Else
MsgBox "Timer already started.", vbInformation, "Timer"
End If
End Sub
Public Sub StopTimer()
If m_TimerID <> 0 Then 'check if timer is active
KillTimer 0, m_TimerID 'it's active, so kill it
m_TimerID = 0
End If
End Sub
Private Sub TimerEvent()
'the API calls this procedure
frmTimer.OnTimer
End Sub
Next, create a userform, save it as frmTimer. Add a text box named txtCountdown. Set property ShowModal to False. Paste the following into the form's code window:
Option Explicit 'code for userform "frmTimer"
'requires a textbox named "txtCountdown" and "ShowModal" set to False.
Dim nextTriggerTime As Date
Private Sub UserForm_Initialize()
ScheduleNextTrigger
End Sub
Public Sub UserForm_Terminate()
StopTimer
If schedTime > 0 Then
schedTime = 0
End If
If onTimerStart_MaximizeExcel Then Application.WindowState = xlMaximized 'maximize excel window
Unload Me
End Sub
Private Sub ScheduleNextTrigger() 'sets the "minor" timer (for the countdown)
StartTimer (1000) 'one second
End Sub
Public Sub OnTimer()
'either update the countdown, or fire the "major" timer task
Dim secLeft As Long
If Now >= schedTime Then
OnTimerTask 'run "major" timer task
Unload Me 'close userForm (won't schedule)
Else
secLeft = CLng((schedTime - Now) * 60 * 60 * 24)
If secLeft < 60 Then 'under 1 minute (don't show mm:ss)
txtCountdown = secLeft & " sec"
Else
'update time remaining in textbox on userform
If secLeft > 60 * 60 Then
txtCountdown = Format(secLeft / 60 / 60 / 24, "hh:mm:ss")
Else 'between 59 and 1 minutes remain:
txtCountdown = Right(Format(secLeft / 60 / 60 / 24, "hh:mm:ss"), 5)
End If
End If
If playTickSound Then Beep 16000, 65 'tick sound
End If
End Sub
Download the demo .xksm. here. There are numerous ways this can be customized or adapted to specific needs. I'm going to use it to calculated and display real time statistics from a popular Q&A site in the corner of my screen...
Note that, since it contains VBA macro's, the file could may set off your virus scanner (as with any other non-local file with VBA). If you're concerned, don't download, and instead build it yourself with the information provided.)
How about moving all the code to a 'Timer' module.
Dim nextTriggerTime As Date
Dim timerActive As Boolean
Public Sub StartTimer()
If timerActive = False Then
timerActive = True
Call ScheduleNextTrigger
End If
End Sub
Public Sub StopTimer()
If timerActive = True Then
timerActive = False
Application.OnTime nextTriggerTime, "Timer.OnTimer", Schedule:=False
End If
End Sub
Private Sub ScheduleNextTrigger()
If timerActive = True Then
nextTriggerTime = Now + TimeValue("00:00:01")
Application.OnTime nextTriggerTime, "Timer.OnTimer"
End If
End Sub
Public Sub OnTimer()
Call MainForm.OnTimer
Call ScheduleNextTrigger
End Sub
Now you can call from the mainform:
call Timer.StartTimer
call Timer.StopTimer
To prevent errors, add:
Private Sub UserForm_Terminate()
Call Timer.StopTimer
End Sub
Wich will trigger:
Public Sub OnTimer()
Debug.Print "Tick"
End Sub
Thanks to user1575005 !!
Used the code in a Module to setup a Timer() process:
Dim nextTriggerTime As Date
Dim timerActive As Boolean
Public Sub StartTimer()
Debug.Print Time() & ": Start"
If timerActive = False Then
timerActive = True
Call ScheduleNextTrigger
End If
End Sub
Public Sub StopTimer()
If timerActive = True Then
timerActive = False
Application.OnTime nextTriggerTime, "OnTimer", Schedule:=False
End If
Debug.Print Time() & ": End"
End Sub
Private Sub ScheduleNextTrigger()
If timerActive = True Then
nextTriggerTime = Now + TimeValue("00:00:10")
Application.OnTime nextTriggerTime, "OnTimer"
End If
End Sub
Public Sub OnTimer()
Call bus_OnTimer
Call ScheduleNextTrigger
End Sub
Public Sub bus_OnTimer()
Debug.Print Time() & ": Tick"
Call doWhateverUwant
End Sub
Private Sub doWhateverUwant()
End Sub