Userform Drop down list VBA - vba

All,
I have created a Userform (Userform 3) which I wish to have a basic drop down list of 4 Items - Apples / Oranges / Pears / bananas. (Hard coded in the background VBA) and if none of the selections are selected by the users to exit sub.
Please can someone advise how I would do this?
Inserted picture for reference?

To populate the Fruits_CB combo-box on UserForm_Initialize event use the code below:
Private Sub UserForm_Initialize()
' populate "Fruits_CB" Combo-Box with fruits
With Me.Fruits_CB
.Clear ' clear previous items (not to have "doubles")
.AddItem "Apples"
.AddItem "Oranges"
.AddItem "Pears"
.AddItem "Bananas"
End With
End Sub
If the user is not selecting any item from the list, and then clicks the button, use the Sub below:
Private Sub CommandButton1_Click()
If Me.Fruits_CB.ListIndex = -1 Then
Exit Sub
Else
' Do something
End If
End Sub

Related

Is there a way to add to a combobox list using a textbox and button?

I'm looking for a way to add items to a combobox list without adding the item to the code directly.
UserForm Image
Private Sub UserForm_Initialize()
With ComboBox2
.AddItem ".020"
.AddItem ".030"
.AddItem ".032"
.AddItem ".040"
End With
With ComboBox3
.AddItem "THK"
.AddItem "DIA"
.AddItem "TUBE"
.AddItem "FORGING"
End With
End Sub
I'd like the user to be able to add an Item into the dropdown list without going into the code. Is there a way to add an .AddItem to the combobox?
Unlike ListBoxes, ComboBoxes allow a user to type a new entry directly into the comboBox field. That's the simplest solution, no extra code required!
If, for some reason, you want to make this more complicated, you can add a text field and a command button. After the user types a new entry in the text field, they click on the button which runs this macro:
Private Sub CommandButton1_Click()
ComboBox3.AddItem TextBox1.value
End Sub

How to control the interlinked comboBoxes output?

I have two ComboBoxes in a Userform.
The first Combox- ComboBox1 is updated with Items as soon as the Userform is initialized, as below.
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "A"
.AddItem "B"
End With
End Sub
Now once a value of the ComboxBox1 is selected, it updates the second ComboBox - ComboBox2, as below -
Private Sub ComboBox1_Change()
With ComboBox2
.Clear
.AddItem "P"
.AddItem "Q"
.AddItem "R"
End With
End Sub
And the ComboBox2 on change displays its current value -
Private Sub ComboBox2_Change()
MsgBox ComboBox2.Value
End Sub
Now when I select a value from ComboBox1 for the first time, ComboBox2 is updated and further on selecting a value from CombBox2, I get a popup message with the ComboBox2 value --- This is fine.
Now again I select a different value from ComboBox1, this time I get a blank popup message, (because it cleared the ComboBox2 content).
How can I handle this, as I don't want the MsgBox popup on changing the ComboBox1, I want the popup only on manually selecting values from ComboBox2?
This is happening because once ComboBox2 has a value and you change ComboBox1 (and .clear combobox2 - it changes ComboBox2. Just use this for combobox2 -
Private Sub ComboBox2_Change()
If ComboBox2.ListCount = 0 Then Exit Sub
MsgBox ComboBox2.Value
End Sub

ComboBox, added items not showing up in list when executing code

I got a small problem, I'm adding items to my combobox in my userform but they don't show up in the combobox when I execute the code...
I'm trying the following code
Private Sub period_input_Change()
With period_input
.AddItem "Apple"
.AddItem "Sugar"
End With
End Sub
Also tried this one
Private Sub box_action()
With Sheets(1).period_input
.AddItem "hello"
.AddItem "mongoasd"
End With
End Sub
It simply shows up as an empty column ( no string is there ). Anyone knows the issue?
You probably want to add the items during the initialization phase of the form:
Private Sub UserForm_Initialize()
With Me.period_input
.AddItem "hello"
.AddItem "mongoasd"
End With
End Sub
Otherwise, I see no problem with the code.

Dropdown list (combobox) disappears when saving PowerPoint 2010 document

I created a drop down list in Power Point 2010, which works very well before saving the document. I used the following code in VBA:
Sub AddItemsToSelectedListBox()
Dim oShape As Shape
Set oShape = ActiveWindow.Selection.ShapeRange(1)
With oShape.OLEFormat.Object
' Add items to the list
.AddItem ("Good")
.AddItem ("Better")
.AddItem ("Best")
' You could work with other properties of the list or combo box here as well
End With
End Sub
f5 + close.
By going to the view modus, the drop down list works well. But if I save my power point document (in a .pptm format) and reopen the presentation, the lists don't drop down any more. If I enter the VBA, the code looks like this:
Sub AddItemsToSelectedListBox()
Dim oShape As Shape
Set oShape = ActiveWindow.Selection.ShapeRange(1)
With oShape.OLEFormat.Object
' Add items to the list
.AddItem ("Good")
.AddItem ("Better")
.AddItem ("Best")
' You could work with other properties of the list or combo box here as well
End With
End Sub
Private Sub ComboBox1_Change()
End Sub
The ComboBox_Change() Part is new. (Why?)
Does anyone know, how to generate a dropdown list that survives the saving-process?
Thank you very much!!
The _Change subroutine would be added when you doubleclick the combo box to get into the VBE.
If you load the combo box from a subroutine in a module, it doesn't seem to retain the values when you save and reopen.
If you load the combo box from an event in the box itself, it seems to work as you'd expect.
For example:
Private Sub ComboBox1_GotFocus()
Dim oShape As Shape
Set oShape = ActivePresentation.Slides(1).Shapes("ComboBox1")
With oShape.OLEFormat.Object
.Clear
' Add items to the list
.AddItem ("Good")
.AddItem ("Better")
.AddItem ("Best")
' You could work with other properties of the list or combo box here as well
End With
End Sub

Jump to an item in Listbox

One question, it is possible to jump somehow to a certain index in a specif listbox as in the image below?
I already tried the following code
Listbox.ListIndex = index
But it drives me to the error You've used the ListIndex property incorrectly
One property of my list that might be important to mention.
Row source type : Table/Query
Thank you in advance.
Try ListBox.Selected(index) = True. If it is a multiselect listbox, you also need to loop through the other elements and unselect them in the same way.
Create a standard module with the code
Sub Main()
UserForm1.Show
Unload UserForm1
End Sub
Insert a userform and visually do something like
Go into the userform code and add
Private Sub CommandButton1_Click()
Dim v As Long
For v = 0 To ListBox1.ListCount - 1
If TextBox1 = ListBox1.List(v) Then
ListBox1.Selected(v) = True
End If
Next v
End Sub
Private Sub UserForm_Initialize()
With ListBox1
.AddItem ("text1")
.AddItem ("text2")
.AddItem ("text3")
End With
End Sub
Run Main Macro
Type in the box : text2
The text2 will be selected in the list