Hello Silver,
This worked for me. Here is the code for the UserForm and the Modules. The attached workbook has all these changes.
UserForm code
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_Activate()
Label1.Caption = "Next Show Time: " & Module1.dtmNextTime
Call KeepFormOnTop
End Sub
Private Sub UserForm_Initialize()
Keep_Form_On_Top.xlHwnd = Excel.Application.hwnd
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Dim ret As Long
ret = ShowWindow(xlHwnd, 1)
End Sub
Module - Keep_Form_On_Top
' This variable is initalized in the UserForm_Initialize() event. It holds the hWnd to the Excel Application Window.
Global xlHwnd As Long
' This API call is used to hide or show the Excel Application.
Public Declare Function ShowWindow _
Lib "user32.dll" _
(ByVal hwnd As LongPtr, _
ByVal nCmdShow As Long) _
As Long
' Returns the Window Handle of the Window that is accepting User input.
Private Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Private Declare Function SetWindowPos _
Lib "user32.dll" _
(ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal cx As Long, _
ByVal cy As Long, _
ByVal wFlags As Long) _
As Long
Sub KeepFormOnTop()
Dim ret As Long
Const HWND_TOPMOST As Long = -1
Const SWP_NOMOVE As Long = &H2
Const SWP_NOSIZE As Long = &H1
ret = ShowWindow(xlHwnd, 0)
ret = SetWindowPos(GetForegroundWindow(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE)
End Sub
Module - Module1
Global dtmNextTime As Date
Global lngRow As Long
Const strSheet = "Sheet1"
Sub StartIt()
lngRow = Evaluate("MATCH(NOW()-TODAY(),'[" & ThisWorkbook.Name & "]" & strSheet & "'!B:B)")
End Sub
Sub SetTimer()
lngRow = lngRow + 1
dtmNextTime = ThisWorkbook.Worksheets(strSheet).Range("B" & lngRow).Value
If dtmNextTime > 0 Then
Application.OnTime Date + dtmNextTime, "ShowForm"
End If
End Sub
Sub ShowForm()
Call StartIt
UserForm1.Show
SetTimer
End Sub