How to control the interlinked comboBoxes output? - vba

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

Related

Reinitialize combo box item selected in userform

How can I load in the answer (item) selected previously for a combobox in a userform.
For instance lets say I had combobox1 that has items below loaded upon initialization:
With ComboBox1
.AddItem "Yes"
.AddItem "No"
End With
How could I load the selected item back in upon reinitialization of the form, so if a user selected his answer, saved, exited the userform and word doc, then reopened the userform. Basically I want the user to see their selection previously chosen to be loaded into combobox1
I have this working for textboxes attached to docvariables, but the comboboxes are attached to bookmarks.
The textboxes reinitializing code is shown below:
Private Sub UserForm_Initialize()
Dim oVars As Object
Set oVars = ActiveDocument.Variables
On Error Resume Next
TextBox1.Value = oVars("bmtitle").Value
Comboboxes are setup like:
Private Sub CommandButton1_Click()
Dim ComboBox1 As Range
Set ComboBox1 = ActiveDocument.Bookmarks("bmextension").Range
ComboBox1.Text = Me.ComboBox1.Value
If Me.ComboBox1.Value = "Yes" Then
ComboBox1.Text = "For additional information see CF4"
End If
If Me.ComboBox1.Value = "No" Then
ComboBox1.Text = ""
End If

Word VBA- ComboBox text reset text output

I'm having trouble with combo box's specifically when a user clicks one of the options in the dropdown menu the text is entered into the document but if they made an error and select another option in the combo box it inputs the the text from both times (the error and right answer). How can I set it up so it will get rid of the text from the error.
The code is below:
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "F1"
.AddItem "G2"
.AddItem "R3"
.AddItem "G4"
End With
End Sub
Private Sub ComboBox1_Change()
Dim ComboBox1 As Range
Set ComboBox1 = Doc1.Bookmarks("bmc1").Range
ComboBox1.Text = Me.ComboBox1.Value
Set ComboBox1 = Doc2.Bookmarks("bmc1").Range
ComboBox1.Text = Me.ComboBox1.Value
End Sub
Your problem is that you are creating a new instance of the bookmark bmc1 every time you call the Change() function, and so a new bookmark is created, and this is where the new text is inserted.
Make the bookmark into a Public variable, and initialize it in the Initialize() function.
Public CBR As Range
Private Sub ComboBox1_Change()
CBR.Text = Me.ComboBox1.Value
End Sub
Private Sub UserForm_Initialize()
Set CBR = ActiveDocument.Bookmarks("bmc1").Range
With ComboBox1
.AddItem "F1"
.AddItem "G2"
.AddItem "R3"
.AddItem "G4"
End With
End Sub

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

Userform Drop down list 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

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.