Hello and thank you for any attention my post may receive.
There are two fields on my userform which are the focus of this process - ComboBox1 and TextBox1. ComboBox1 contains Accountable Leader roles.
The code is assigned to TextBox1 - upon exit of TextBox1 the code triggers and validates two things - 1. IF ComboBox1 has a value or not, 2. IF ComboBox1 does a value, check whether TextBox1 value already exists on ws 'Project_List' for the role selected in ComboBox1.
My aim is to trigger the code whenever the user exits TextBox1 by one of three methods - ENTER, TAB, or the user clicks out. So far my code triggers on ENTER and TAB; I am unsure how to achieve my third aim.
This is my code so far:
Any help or advice would be greatly appreciated.
Have a great day!
There are two fields on my userform which are the focus of this process - ComboBox1 and TextBox1. ComboBox1 contains Accountable Leader roles.
The code is assigned to TextBox1 - upon exit of TextBox1 the code triggers and validates two things - 1. IF ComboBox1 has a value or not, 2. IF ComboBox1 does a value, check whether TextBox1 value already exists on ws 'Project_List' for the role selected in ComboBox1.
My aim is to trigger the code whenever the user exits TextBox1 by one of three methods - ENTER, TAB, or the user clicks out. So far my code triggers on ENTER and TAB; I am unsure how to achieve my third aim.
This is my code so far:
Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 13:
If Me.ComboBox1.value = "" Then
temp = MsgBox("You must select the appropriate 'Accountable Leader' first.", vbCritical + vbOKOnly, "You're in the wrong field!")
Me.TextBox1 = vbNullString
KeyCode = 0
ComboBox1.SetFocus
Else
With Worksheets("Project_List")
If (WorksheetFunction.CountIf(.Columns(4), Me.TextBox1.value) _
+ WorksheetFunction.CountIf(.Columns(3), Me.ComboBox1.value)) > 0 Then
MsgBox "Specific location '" & Me.TextBox1.value & "' already exists for '" & Me.ComboBox1.value & "'..."
TextBox1 = vbNullString
KeyCode = 0
End If
End With
End If
Case 9:
If Me.ComboBox1.value = "" Then
temp = MsgBox("You must select the appropriate 'Accountable Leader' first.", vbCritical + vbOKOnly, "You're in the wrong field!")
Me.TextBox1 = vbNullString
KeyCode = 0
ComboBox1.SetFocus
Else
With Worksheets("Project_List")
If (WorksheetFunction.CountIf(.Columns(4), Me.TextBox1.value) _
+ WorksheetFunction.CountIf(.Columns(3), Me.ComboBox1.value)) > 0 Then
MsgBox "Specific location '" & Me.TextBox1.value & "' already exists for '" & Me.ComboBox1.value & "'..."
TextBox1 = vbNullString
KeyCode = 0
End If
End With
End If
End Select
End Sub
Any help or advice would be greatly appreciated.
Have a great day!
Last edited: