Hi all, I am a real newbie to vba, however I am developing a userform to show staff names (populated in a combobox through row source) and contact numbers on the top portion, and the bottom of the form contains buildings (in the first list box) that show occupants (in the second list box) that will then show contact numbers. All contact numbers are displayed in text boxes. When I hit the Clear button, only the text boxes clear, and no matter what I try, the combobox and the listbox stay populated. Also, is there any way of stopping an error (debug) from occurring when a name is misspelt or you backspace to a blank in the combobox. I've tried adding in a blank row to no avail - and I wouldn't have a clue how to add a wildcard entry if that would work. Thanks in advance for any help. Everything else works fine - I'm pretty chuffed with what I have achieved thus far - Code is attached - if you need the complete file - let me know.
Code
Private Sub cbAdd_Click()
UserForm1.Show
Unload Me
End Sub
Private Sub cbExit_Click()
ThisWorkbook.Save
Application.Quit
End Sub
Private Sub cboName_Change()
TextBox5.Text = Application.VLookup(cboName.Value, Worksheets("Department").Range("A1:F130"), 2, False)
TextBox6.Text = Application.VLookup(cboName.Value, Worksheets("Staff").Range("A1:F130"), 3, False)
TextBox7.Text = Application.VLookup(cboName.Value, Worksheets("Staff").Range("A1:F130"), 4, False)
Textbox8.Text = Application.VLookup(cboName.Value, Worksheets("Staff").Range("A1:F130"), 5, False)
TextBox9.Text = Application.VLookup(cboName.Value, Worksheets("Staff").Range("A1:F130"), 6, False)
End Sub
Private Sub cbClear_Click()
Dim ctl
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.TextBox Then
ctl.Text = ""
End If
Next ctl
End Sub
Private Sub ListBuilding_Click()
Dim X As Integer
X = ListBuilding.ListIndex
Select Case X
Case Is = 0
ListStaff.RowSource = "staff1"
Case Is = 1
ListStaff.RowSource = "staff3"
Case Is = 2
ListStaff.RowSource = "staff6"
Case Is = 3
ListStaff.RowSource = "staff11"
Case Is = 4
ListStaff.RowSource = "staff19"
Case Is = 5
ListStaff.RowSource = "staff21"
Case Is = 6
ListStaff.RowSource = "staff22"
Case Is = 7
ListStaff.RowSource = "staff24"
Case Is = 8
ListStaff.RowSource = "staff51"
Case Is = 9
ListStaff.RowSource = "staff80"
Case Is = 10
ListStaff.RowSource = "staff82"
End Select
End Sub
Private Sub ListStaff_Click()
TextBox1.Text = Application.VLookup(ListStaff.Value, Worksheets("staff").Range("A1:F130"), 4, False)
TextBox2.Text = Application.VLookup(ListStaff.Value, Worksheets("staff").Range("A1:F130"), 5, False)
TextBox3.Text = Application.VLookup(ListStaff.Value, Worksheets("staff").Range("A1:F130"), 6, False)
TextBox4.Text = Application.VLookup(ListStaff.Value, Worksheets("staff").Range("A1:F130"), 3, False)
End Sub
Private Sub UserForm_Initialize()
ListBuilding.List = Worksheets("Staff").Range("A2:F130").Value
ListBuilding.RowSource = "Building"
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Please use the Quit PhoneBook button to close the Phone Book", vbOKOnly
End If
End Sub
Display More
Code/