Option Explicit
Dim d As Object
Dim e As Object
Dim va
Private Sub btnCancel_Click()
Unload Me
End Sub
Private Sub btnClear_Click()
Call UserForm_Initialize
End Sub
Private Sub toPopulate(n As Long)
Dim i As Long
Dim tx As String
Dim x
d.RemoveAll
e.RemoveAll
For i = 1 To 6
tx = Me.Controls("Combobox" & i).Value
If tx <> "" And i <> n Then e(tx) = Empty
Next
If e.Count <> 0 Then
For Each x In va
If Not e.Exists(x) Then d(x) = Empty
Next
Else
For Each x In va
d(x) = Empty
Next
End If
Me.Controls("Combobox" & n).List = d.keys
End Sub
Private Sub ComboBox1_Enter()
Call toPopulate(1)
End Sub
Private Sub ComboBox2_Enter()
Call toPopulate(2)
End Sub
Private Sub ComboBox3_Enter()
Call toPopulate(3)
End Sub
Private Sub ComboBox4_Enter()
Call toPopulate(4)
End Sub
Private Sub ComboBox5_Enter()
Call toPopulate(5)
End Sub
Private Sub ComboBox6_Enter()
Call toPopulate(6)
End Sub
Private Sub TextBox1_AfterUpdate()
Dim tString As String
With TextBox1
'Check if user put in a colon or not
If InStr(1, .Value, ":", vbTextCompare) = 0 Then
'If not, make string 4 digits and insert colon
tString = Format(.Value, "0000")
tString = Left(tString, 2) & ":" & Right(tString, 2)
TextBox1.Value = Format(TimeValue(tString), "HH:MM AM/PM")
Else
'Otherwise, take value as given
.Value = Format(.Value, "hh:mm AM/PM")
End If
End With
End Sub
Private Sub TextBox2_AfterUpdate()
Dim tString As String
With TextBox2
'Check if user put in a colon or not
If InStr(1, .Value, ":", vbTextCompare) = 0 Then
'If not, make string 4 digits and insert colon
tString = Format(.Value, "0000")
tString = Left(tString, 2) & ":" & Right(tString, 2)
TextBox2.Value = Format(TimeValue(tString), "HH:MM AM/PM")
Else
'Otherwise, take value as given
.Value = Format(.Value, "hh:mm AM/PM")
End If
End With
End Sub
Private Sub TextBox3_AfterUpdate()
Dim tString As String
With TextBox3
'Check if user put in a colon or not
If InStr(1, .Value, ":", vbTextCompare) = 0 Then
'If not, make string 4 digits and insert colon
tString = Format(.Value, "0000")
tString = Left(tString, 2) & ":" & Right(tString, 2)
TextBox3.Value = Format(TimeValue(tString), "HH:MM AM/PM")
Else
'Otherwise, take value as given
.Value = Format(.Value, "hh:mm AM/PM")
End If
End With
End Sub
Private Sub TextBox4_AfterUpdate()
Dim tString As String
With TextBox4
'Check if user put in a colon or not
If InStr(1, .Value, ":", vbTextCompare) = 0 Then
'If not, make string 4 digits and insert colon
tString = Format(.Value, "0000")
tString = Left(tString, 2) & ":" & Right(tString, 2)
TextBox4.Value = Format(TimeValue(tString), "HH:MM AM/PM")
Else
'Otherwise, take value as given
.Value = Format(.Value, "hh:mm AM/PM")
End If
End With
End Sub
Private Sub txtbxDate_AfterUpdate()
Dim todaysDate As String
todaysDate = Format(Me.txtbxDate.Value, "long date")
If IsDate(Me.txtbxDate.Text) Then
Me.txtbxDate.Text = todaysDate
End If
End Sub
Private Sub txtbxDate_Enter()
Me.txtbxDate.Value = ""
End Sub
Private Sub UserForm_Initialize()
' Dim ctrl As Control
'
' For Each ctrl In UserForm1.Controls
' If TypeName(ctrl) = "TextBox" Then ctrl.Value = ""
' If TypeName(ctrl) = "ComboBox" Then
' ctrl.List = Sheets("Employees").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Value
' End If
' Next
Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
Set e = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
With Sheets("Employees")
va = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
End With
End Sub