VBA ComboBox not displaying values [duplicate] - vba

This question already has answers here:
My ComboBox doesn't display the values I've added in VBA
(3 answers)
Closed 5 years ago.
Trying to use the ComoboBox with VBA and it only displays 1 empty row when you click on the down arrow. I have never used ComboBox before and am still a beginner to VBA.
Private Sub ComboBoxT_Change()
ComboBoxT.AddItem "apple"
ComboBoxT.AddItem "orange"
ComboBoxT.AddItem "banana"
End Sub

Like Saagar said, they way that the code is currently written, it will only add the items into your combobox if there is a change in the combobox. The code that I would use would look like this:
Private Sub UserFormName_Activate()
ComboBoxT.AddItem "apple"
ComboBoxT.AddItem "orange"
ComboBoxT.AddItem "banana"
end sub
The easiest way to achieve this is in the user form creator, double click on the userform frame. This should show the code you would be working with, then from the top dropdown bars (the first one might say either "UserForm" or "General") you can select "Userform" in the first one and "Activate" in the second one. That should give you a good place to start, and the items you wanted added to the ComboBox should be right there, ready to go. Hope this helps :)

The Event you selected to add items to the ComboBox will only be fired if there is a change in the ComboBox. You need to enter this piece of code to add new items to the ComboBox in another event which is fired before you want to use the ComboBox.
Most cases, it is done at the Form_Load event, so that you have the items available inside the ComboBox by the time the Form is loaded completely.
Try the below
Private Sub Form_Load()
ComboBoxT.AddItem "apple"
ComboBoxT.AddItem "orange"
ComboBoxT.AddItem "banana"
End Sub

Related

Get value from command button VBA

I have created A userform with few command buttons.
I can't seem to figure out how do I get the information from them.
I want to open this userform from another one and then I want the user to choose an option from one of the buttons which will change a specific cell in the table itself
the userform I created
I did not write anything on this userform therefor there is no code from it to show only the design.
how do get the information from the buttons to be written in A specific cell on a specific worksheet?
double click on one of the buttons, in the code menu a new sub should appear. this looks something like:
Sub CommandButton1_onClick()
End sub
Alongside this event method, it also has a few properties. The one that is usefull here is the CommandButton1.Value, this represents the state of the button (either true or false iirc).
So build for each button (I strongly advice giving them custom names to prevent getting lost in the trouble) as sub like this:
Sub CommandButton1_onClick()
if CommandButton1.Value then
ThisWorkBook.WorkSheets("WorksheetName").Range("YourRange").Value = "Some value"
else
ThisWorkBook.WorkSheets("WorksheetName").Range("YourRange").Value = ""
end if
End sub

Hide DropDown list for ComboBox without losing focus

I have created a userform similar to a google search. I'd like the drop down to show/hide depending on how many letters are in value.
Like this..
If Len(ComboBox1.Value) > 4 Then
ComboBox1.DropDown
Else
'ComboBox.DroppedDown = False (non-existent method)
End If
There are many versions of this question, most result in changing focus, but this should occur while the box is being typed in. I find it hard to believe it's not possible..
EDIT: Question pertains to hiding the list below, not the arrow to the side - as it is set to always hidden - the list should hide/unhide based on length of value.
The closest I can get is this, but for some reason if you delete characters it doesn't update. Plus it only starts working once there are more than 5 characters! Edit - amended as per Mistella's comments.
Private Sub ComboBox1_Change()
If Len(Me.ComboBox1.Value) > 3 Then
Me.ComboBox1.ShowDropButtonWhen = fmShowDropButtonWhenAlways
Else
Me.ComboBox1.ShowDropButtonWhen = fmShowDropButtonWhenNever
End If
End Sub
Private Sub UserForm_Initialize()
With Me.ComboBox1
.AddItem "A"
.AddItem "B"
.AddItem "C"
.AddItem "D"
.ShowDropButtonWhen = fmShowDropButtonWhenNever
End With
End Sub
If the answer is still important for you, I've managed to avoid showing the dropdown by consequent disabling and enabling the control.
In my case I wanted the combobox to call another userform by click on the arrow with subsequent pasting the result into the texbox part of combo. I also was irritated by the dropdown appearing.
I'm not sure it will do you any help, as subsequent .SetFocus causes dropdown to appear again..., but just in case, following helped me:
UserForm1.ComboBox1.Enabled = False
UserForm1.ComboBox1.Enabled = True

move items to the another listbox which is searched by textbox

I am beginner at programming. I'm trying to build a form based app using visual basic according to a example in youtube.
In the form I cant move a item from listbox1 to listbox2 which was searched in textbox2
When I write the first item which is placed in first row of listbox1 it moves item to listbox2 but if I try with another item it cant move to listbox2.
I would be happy if someone can help me about this case.
Here are a picture of my form and the code I use :
a Lot depends on what you are putting into the listboxes. If it is simple stuff like a, b, c, d etc it will add everything to listbox 2 as you have your code under text_changed event. Try and use a button when user stopped typing to search for the entire word.
Also add your text after the sc Call to your sc sub before the return statement as well -
Sub sc()
''Current code
''If Textbox2<text - remove, already called...
Listbox2.Items.Add(Listbox1.Text)
Listbox1.Items.Remove(Listbox1.SelectedIndex)
Return
End Sub

Adding text to a combobox

Currently I have a simple ComboBox that populates with 3 items when clicked:
With FunctionBox
.AddItem "Add Blank Issue"
.AddItem "Move Existing Issue"
.AddItem "Reorder Issues"
End With
However, the combobox is empty before the dropdown arrow is selected.
When the combobox appears I want it to show something like, "Please select one of the options below".
I tried setting the 'Value' of the combobox to 'test'. Test shows up in the editor, but does not when I run the application.
I also want to make sure the string goes away when the down arrow is selected and the user cannot interact with it.
Any advice?
I can provide screenshots if this is somewhat unclear.
Thanks!
Goto design view of your form, click on the combo box in question and change the text value to what you want it to say.
Edit - didnt see the 2nd part of your question
put this in the dropbuttonclick event. changing combobox1 where needed
Private Sub ComboBox1_DropButtonClick()
ComboBox1.value = ""
End Sub
To have a default value in combobox, add this code in UserForm_Initialize():
FunctionBox.AddItem "Please select one of the options below"

My ComboBox doesn't display the values I've added in VBA

I'm trying to add options to a combo box in a userform. When I run the code, Excel doesn't give any errors, however when the userform shows up it doesn't display the entities I have added to the combobox previously. That is, when I click on the combobox, it doesn't show any options, only one blank row, as if no items were added to it.
Here is the code I'm using:
Private Sub UserForm_Initialize()
ComboBox1.AddItem "xxx"
ComboBox1.AddItem "yyy"
ComboBox1.AddItem "zzz"
End Sub
I am using the following code to call the user form within a macro:
UserForm.Show
The code given in the question works perfectly well. In my case the code didn't work because I manually entered this part of the code into VBA:
Private Sub UserForm_Initialize()
If you make Excel create this module for you instead of writing it on your own, your code should work perfectly. Excel did not have "Initialize" as a default form so I tried "Activate" and it worked.
To create this module you have to do the following steps:
Right click on user form
Click on view code
On top you will see two categories you can pick, you should pick "Userform" and "Activate", after completing this step excel must add a new module to your code.
In this module you may code everything you want about the content of the combobox.
You should also be careful with the spelling of your combobox, if you spell it incorrectly, you may be unable to see the contents of the combobox.
Ensure that the code segment you have posted is in the userform.
Right click on the user form in the VBA view and choose "View Code". Is this where the code is?
Are you sure that the User Form is called 'UserForm' and not 'UserForm1'? 'UserForm1' is the default, similar to 'ComboBox1'.
The below works for me.
'in the UserForm1 code
Private Sub UserForm_Initialize()
ComboBox1.AddItem "xxx"
ComboBox1.AddItem "yyy"
ComboBox1.AddItem "zzz"
End Sub
The below will display the form.
UserForm1.Show
Is this the only form in the workbook? Create a new one and see if it does the same thing.
Same problem here, but I solved setting ColumnWidths property to -1 (it was set to 0).