Hi all,
I'm new to the forum and relatively new to VBA coding. I could really use some help.
I have a workbook with a timer that has start, stop and save buttons. My problem is that when the workbook is saved and reopened, the timer restarts at zero when the Start button is pressed. I need a pause and resume button instead of a Stop so that when the workbook is reopened the timer can resume where it left off. I've tried a half a dozen things on my own and have had zero luck achieving this end. Any help is appreciated.
Here is my current code:
Dim StopTimer As Boolean
Dim SchdTime As Variant
Dim Etime As Date
Const OneSec As Date = 1 / 86400#
Private Sub StartBtn_Click()
Application.Cursor = xlNorthwestArrow
StartBtn.BackColor = vbGreen
StopBtn.BackColor = vbWhite
StopTimer = False
SchdTime = Now() + TimeValue("00:00:00")
[h6].Value = Format(Etime, "hh:mm:ss")
Application.OnTime SchdTime + OneSec, "Sheet1.NextTick"
End Sub
Private Sub StopBtn_click()
StopBtn.BackColor = vbRed
StartBtn.BackColor = vbWhite
StopTimer = True
Beep
End Sub
Sub NextTick()
If StopTimer Then
'Don't reschedule update
Else
SchdTime = SchdTime + OneSec
[h6].Value = Format(Etime, "hh:mm:ss")
Application.OnTime SchdTime, "Sheet1.NextTick"
Etime = Etime + OneSec
End If
End Sub
Sub GoSheetNext()
Dim wb As Workbook
Dim lSheets As Long
Dim lSheet As Long
Dim lMove As Long
Dim lNext As Long
Set wb = ActiveWorkbook
lSheets = wb.Sheets.Count
lSheet = ActiveSheet.Index
lMove = 1
With wb
For lMove = 1 To lSheets - 1
lNext = lSheet + lMove
If lNext > lSheets Then
lMove = 0
lNext = 1
lSheet = 1
End If
If .Sheets(lNext).Visible = True Then
.Sheets(lNext).Select
Exit For
End If
Next lMove
End With
End Sub
Sub GoSheetBack()
Dim wb As Workbook
Dim lSheets As Long
Dim lSheet As Long
Dim lMove As Long
Dim lNext As Long
Set wb = ActiveWorkbook
lSheets = wb.Sheets.Count
lSheet = ActiveSheet.Index
lMove = 1
With wb
For lMove = 1 To lSheets - 1
lNext = lSheet - lMove
If lNext < 1 Then
lMove = 0
lNext = lSheets
lSheet = lSheets
End If
If .Sheets(lNext).Visible = True Then
.Sheets(lNext).Select
Exit For
End If
Next lMove
End With
End Sub
'Save as Excel and PDF
Sub SaveBtn_click()
ChDir "c:\Users\Public\PublicDocuments"
ActiveWorkbook.SaveAs Filename:=Range("h4").Value, FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ChDir "c:\Users\Public\PublicDocuments"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Range("c8").Value, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Application.DisplayAlerts = False
ActiveWorkbook.Close True
Application.DisplayAlerts = True
End Sub
Bookmarks