Display data in text box between certain times

stuartchaffey

New Member
Joined
Dec 28, 2021
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi,
I would like to display a certain text in a text box on a userform if it is between certain times. is this possible?
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
This might work for you :

In the example below, the textbox will show the text "hello" if the time is between 4pm and 4:30pm otherwise no text. Change times to suit your needs.

In the UserForm Module:
VBA Code:
Option Explicit

Private bMonitoring As Boolean

Private Sub UserForm_Activate()
    Call MonitorTime
End Sub

Private Sub UserForm_Terminate()
    bMonitoring = False
End Sub

Private Function TimeBetween(ByVal t1 As Date, ByVal t2 As Date) As Boolean
    TimeBetween = CBool(t1 <= Time And t2 >= Time)
End Function

Private Sub MonitorTime()
    If Not bMonitoring Then
        bMonitoring = True
        Do While bMonitoring
            If TimeBetween(#4:00:00 PM#, #4:30:00 PM#) Then
                Me.TextBox1 = "hello"
            Else
                Me.TextBox1 = ""
            End If
            DoEvents
        Loop
    End If
End Sub
 
Upvote 0
Solution
Thank you, works a dream. If i wanted to put more than 2 different times for the same text box is that possible?. for example in textbox1 if its between 8&9 display online, and if its between 9&10 display offline?
 
Upvote 0
Thank you, works a dream. If i wanted to put more than 2 different times for the same text box is that possible?. for example in textbox1 if its between 8&9 display online, and if its between 9&10 display offline?
VBA Code:
Private Sub MonitorTime()
    If Not bMonitoring Then
        bMonitoring = True
        Do While bMonitoring
            If TimeBetween(#8:00:00 AM#, #9:00:00 AM#) Then
                Me.TextBox1 = "Online"
            ElseIf TimeBetween(#9:00:00 AM#, #10:00:00 AM#) Then
                Me.TextBox1 = "Offline"
            Else
                Me.TextBox1 = "Unavailable"
            End If
            DoEvents
        Loop
    End If
End Sub

Note the AM\PM 12 hour clock system.
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,726
Members
449,093
Latest member
Mnur

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