Excel Userform List keeps repeating itself

Candyman7

New Member
Joined
Jan 31, 2020
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
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
 
If you have a long list of values you want loaded into a control and do not want to write this all out you could load the Combobox like this:
This would load all the values in a range into a Combobox
In this example all the values in the active sheet in Range("A1:A10") would be loaded into the ComboBox
VBA Code:
Private Sub UserForm_Initialize()
'Modified  1/31/2020  5:02:26 AM  EST
Me.ComboBox1.List = Range("A1:A10").Value
End Sub
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Forum statistics

Threads
1,223,923
Messages
6,175,410
Members
452,640
Latest member
steveridge

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top