Hi guys, I have the below vba code for a userform. Every time I select the list, it keeps repeating itself and keeps getting longer. Please review the below code and provide suitable solutions. Thank you.
Private Sub cboClass_DropButtonClick()
'Populate control.
Me.cboClass.AddItem "Mammals"
Me.cboClass.AddItem "Reptiles"
Me.cboClass.AddItem "Birds"
Me.cboClass.AddItem "Amphibians"
Me.cboClass.AddItem "Fish"
End Sub
Private Sub cboConservationStatus_DropButtonClick()
'Populate control.
Me.cboConservationStatus.AddItem "Endangered"
Me.cboConservationStatus.AddItem "Extinct"
Me.cboConservationStatus.AddItem "Nearly Extinct"
Me.cboConservationStatus.AddItem "Stable"
Me.cboConservationStatus.AddItem "Threatened"
Me.cboConservationStatus.AddItem "Critically Endangered"
Me.cboConservationStatus.AddItem "Overpopulated"
End Sub
Private Sub cboSex_DropButtonClick()
'Populate control.
Me.cboSex.AddItem "Male"
Me.cboSex.AddItem "Females"
End Sub
Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim lRow As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(lRow, 1).Value = Me.cboClass.Value
.Cells(lRow, 2).Value = Me.cboSex.Value
.Cells(lRow, 3).Value = Me.cboConservationStatus.Value
.Cells(lRow, 4).Value = Me.txtComment.Value
End With
'Clear input controls.
Me.cboClass.Value = ""
Me.cboSex.Value = ""
Me.cboConservationStatus.Value = ""
Me.txtComment.Value = ""
End Sub
Private Sub cmdClose_Click()
'Close UserForm.
Unload Me
End Sub
Private Sub Class_Click()
Me.cboConservationStatus.Clear
Me.cboSex.Clear
End Sub
Private Sub cboClass_DropButtonClick()
'Populate control.
Me.cboClass.AddItem "Mammals"
Me.cboClass.AddItem "Reptiles"
Me.cboClass.AddItem "Birds"
Me.cboClass.AddItem "Amphibians"
Me.cboClass.AddItem "Fish"
End Sub
Private Sub cboConservationStatus_DropButtonClick()
'Populate control.
Me.cboConservationStatus.AddItem "Endangered"
Me.cboConservationStatus.AddItem "Extinct"
Me.cboConservationStatus.AddItem "Nearly Extinct"
Me.cboConservationStatus.AddItem "Stable"
Me.cboConservationStatus.AddItem "Threatened"
Me.cboConservationStatus.AddItem "Critically Endangered"
Me.cboConservationStatus.AddItem "Overpopulated"
End Sub
Private Sub cboSex_DropButtonClick()
'Populate control.
Me.cboSex.AddItem "Male"
Me.cboSex.AddItem "Females"
End Sub
Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim lRow As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(lRow, 1).Value = Me.cboClass.Value
.Cells(lRow, 2).Value = Me.cboSex.Value
.Cells(lRow, 3).Value = Me.cboConservationStatus.Value
.Cells(lRow, 4).Value = Me.txtComment.Value
End With
'Clear input controls.
Me.cboClass.Value = ""
Me.cboSex.Value = ""
Me.cboConservationStatus.Value = ""
Me.txtComment.Value = ""
End Sub
Private Sub cmdClose_Click()
'Close UserForm.
Unload Me
End Sub
Private Sub Class_Click()
Me.cboConservationStatus.Clear
Me.cboSex.Clear
End Sub