Hello masters. Below is some simple code for populating my listbox using one combobox. My issues are:
Right now, I don't have the correct sheet declaration, so how would I do this? For example, if I'm on Sheet1, the combobox shows the range for Sheet1 and not Sheet7 (Distributions).
When I save a new entry based on my combobox selection, it also errors. This also errors if I delete any value in the combobox field after activation (obviously). How do I allow the user to clear the field (by deletion) without giving an error?
Any help would be greatly appreciated!
Right now, I don't have the correct sheet declaration, so how would I do this? For example, if I'm on Sheet1, the combobox shows the range for Sheet1 and not Sheet7 (Distributions).
When I save a new entry based on my combobox selection, it also errors. This also errors if I delete any value in the combobox field after activation (obviously). How do I allow the user to clear the field (by deletion) without giving an error?
Any help would be greatly appreciated!
Code:
[/COLOR][COLOR=#333333]Private Sub UserForm_Initialize()[/COLOR]
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">
' This sub is used to fill Combobox at userform initializing
With Sheets("Distributions").Range("A:A")
LastRow = .Cells(.Count, 1).End(xlUp).Row
End With
With WorksheetFunction
For j = 2 To LastRow
If .CountIf(Range("A2:A" & j), Range("A" & j)) = 1 Then
ComboBox1.AddItem Range("A" & j).Value
End If
Next
End With
End Sub
Private Sub ComboBox1_Change()
' Determining what is selected in combobox
With ComboBox1
SelectedDIST = .List(.ListIndex)[COLOR=#FF0000] '\\error 381: could not get list property. invalid array index[/COLOR]
End With
' Getting LastRow number
With Range("A:A")
LastRow = .Cells(.Count, 1).End(xlUp).Row
End With
' Filling ListBox
ListBox1.Clear
For Each cell In Range("A2:A" & LastRow)
If cell.Value = SelectedDIST Then
ListBox1.AddItem cell.Offset(0, 1)
End If
Next
End Sub
Private Sub cmdAdd_Click()
Dim MyValue As Long
With ThisWorkbook.Sheets(msSHEET_NAME)
'check for formation name
If Trim(Me.TextBox1.Value) = "" Then
Me.TextBox1.SetFocus
MsgBox "Please enter an email address."
Exit Sub
End If
'check valid entries of text boxes
If TextBox1.Value = "" Then
MsgBox "You must enter an email address."
Exit Sub
End If
If TextBox1.Value = -1 Then
MsgBox "You must enter an email address."
Exit Sub
End If
If ComboBox1.Value = "" Then
MsgBox "You must enter a distribution group."
Exit Sub
End If
If ComboBox1.Value = -1 Then
MsgBox "You must enter a distribution group."
Exit Sub
End If
End With
'values entered are stored in sheet into first available empty row
With ThisWorkbook.Sheets(msSHEET_NAME)
miRowNo_Last = .Range(msTEST_COLUMN & .Rows.Count).End(xlUp).Row + 1
.Cells(miRowNo_Last, miCOL_NO__EG) = Me.ComboBox1.Text
.Cells(miRowNo_Last, miCOL_NO__GE) = Me.TextBox1.Text
End With
'sort emails alphabetically
LastRow = Cells(Rows.Count, 2).End(xlUp).Row
Range("A2:B" & LastRow).Sort key1:=Range("B2:B" & LastRow), _
order1:=xlAscending, Header:=xlNo
'clear the data
Me.TextBox1.Value = ""
Me.ComboBox1.Value = "" </code>[COLOR=#333333]End Sub[/COLOR][COLOR=#333333]