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
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I would do it this way.
The Cbo box is loaded when userform is opened.
Put this script in your userform. Not in the control.

VBA Code:
Private Sub UserForm_Initialize()
'Modified  1/31/2020  3:47:53 AM  EST
With Me.cboClass
    .Clear
    .AddItem "Mammals"
    .AddItem "Reptiles"
    .AddItem "Birds"
    .AddItem "Amphibians"
    .AddItem "Fish"
End With
End Sub
 
Upvote 0
I would do it this way.
The Cbo box is loaded when userform is opened.
Put this script in your userform. Not in the control.

VBA Code:
Private Sub UserForm_Initialize()
'Modified  1/31/2020  3:47:53 AM  EST
With Me.cboClass
    .Clear
    .AddItem "Mammals"
    .AddItem "Reptiles"
    .AddItem "Birds"
    .AddItem "Amphibians"
    .AddItem "Fish"
End With
End Sub


Thank you for your reply, I tried modifying the code as you suggested. However, I am now not able to select any option at all. The list displays the classes, but am not able to select it. Not sure what the problem is now.
 
Upvote 0
Remove any code you have in the cbo class

I assume this is a combobox.

So you have the values added into the combobox.
Automatically when you open Userform.
What do you want to happen when you choose "Birds"
 
Upvote 0
Remove any code you have in the cbo class

I assume this is a combobox.

So you have the values added into the combobox.
Automatically when you open Userform.
What do you want to happen when you choose "Birds"

Yes, Just realised there was some redundant code before the cbo class, works like a charm now. Thank you for your help.
 
Upvote 0
And you said:
I tried modifying the code as you suggested
I wanted you to completely remove the code you had in the control and put my code in the Userform
now it runs automatically when userform is opened
Double click on userform and post in my code
 
Upvote 0
Yes, Just realised there was some redundant code before the cbo class, works like a charm now. Thank you for your help.
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.
 
Upvote 0
And you said:
I tried modifying the code as you suggested
I wanted you to completely remove the code you had in the control and put my code in the Userform
now it runs automatically when userform is opened
Double click on userform and post in my code

Yes, your code was only for cboClass, I wanted to update it for cboConservationStatus and cboSex as well.
 
Upvote 0
I figured you would see how to do it and could add the others like this for example:

VBA Code:
Private Sub UserForm_Initialize()
With Me.cboClass
    .Clear
    .AddItem "Mammals"
    .AddItem "Reptiles"
    .AddItem "Birds"
    .AddItem "Amphibians"
    .AddItem "Fish"
End With
With Me.cboConservationStatus
    .Clear
    .AddItem "One"
    .AddItem "Two"
    .AddItem "Thee"
    .AddItem "Four"
    .AddItem "Five"
End With
End Sub

And on and on as many as you need.
 
Upvote 0
I figured you would see how to do it and could add the others like this for example:

VBA Code:
Private Sub UserForm_Initialize()
With Me.cboClass
    .Clear
    .AddItem "Mammals"
    .AddItem "Reptiles"
    .AddItem "Birds"
    .AddItem "Amphibians"
    .AddItem "Fish"
End With
With Me.cboConservationStatus
    .Clear
    .AddItem "One"
    .AddItem "Two"
    .AddItem "Thee"
    .AddItem "Four"
    .AddItem "Five"
End With
End Sub

Yes, I've successfully updated the entire code. Thank you kind sir.
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,225
Members
453,025
Latest member
Hannah_Pham93

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