ComboBox list throgh Range in sheet - excel

In userform I inserted ComboBox and i have to add list through Array following are the code. I want that whether it is possible that in ComboBox list will get from a Range in sheet (like in case of Data Validation)
Private Sub UserForm_Initialize()
ComboBox1.List = Array("Item1", "Item2", "Item3", "Item4")
End Sub

you can use the range in the sheet to create, even a named range, e.g.:
combobox1.list = range(cells(1,1),cells(100,1)).value
combobox2.list = sheets(1).range("NamedRange")
arr = array("1","2","3")
combobox3.list = arr

This is how you'd use a range to set the row source of a combo box. You can also do this in vba.
Me.combobox1.RowSource = "MyRange"

You can set this to a Table (ListObject) instead of a static range. Create a Table with your list of values, instead of just using a static range of cells. This way when you need to add to the list, you simply enter the new values, which are added to the Table. In my example, I have a Table named "Table1" and a column with the heading "Numbers". Then call this function:
Private Sub UserForm_Initialize()
ComboBox1.RowSource = "=Table1[Numbers]"
End Sub
You have to do this on the Iniitalize, since setting the RowSource from the ComboBox Properties will cause Excel to crash the first time you add an item to your list.
This gives you a list that you can edit without having to edit the code behind the UserForm.

Related

How to remove item from combobox in a userform?

I have a list of names in a sheet. I set these names as my RowSource for a combobox on a useform.
There are two Comboboxes involved. One starts full, the other starts empty.
I want when I click on a name from the first (full) combobox, said name to be added to the other combobox, and removed from the original combobox (and vice versa eventually).
I can't remove anything with RemoveItem.
I went the 'Menu.ListeAjoutAg.ListIndex' way to get my current selection's index ('Menu' is the UserForm and 'ListeAjoutAg' is the combobox), but it did not work.
Tried inputting through a variable I created real quick, 'b', but same result. No index number works. I checked and I only feed the function integers (0, 1, 3, 4...) that are correct and/or well within the scope of my list (about 45 names).
Private Sub ListeAjoutAg_Change()
a = Menu.ListeAjoutAg.Text
b = Menu.ListeAjoutAg.ListIndex
Menu.ListeRetirer.AddItem (a) ' goes fine till there
Menu.ListeAjoutAg.RemoveItem (b) 'and here it goes wrong
Menu.ListeRetirer.Enabled = True
Menu.ListeRetirer.Visible = True
End Sub
As already mentioned: You can't add or remove items from a Listbox if you have set the Rowsource property.
However, it is rather easy to fill a Listbox from a range - simply copy the cell values into an array and assign the array as List. See for example VBA Excel Populate ListBox with multiple columns
Put the following routine into your form and call it from the form Activate event.
Private Sub fillListBox(r As Range)
Me.ListeAjoutAg.Clear
Me.ListeAjoutAg.ColumnCount = r.Columns.Count
Dim data
data = r.Value
Me.ListeAjoutAg.List = data
End Sub
Private Sub UserForm_Activate()
Dim r As Range
' Replace this with the range where your data is stored.
Set r = ThisWorkbook.Sheets(1).Range("A2:C10")
fillListBox r
End Sub

excel vba listbox additem

I am running office 365 home on windows 10. I am programming Excel using VBA.
I have a data range in a worksheet row. I want the user to be able to select one item from this row of data. I am trying to populate ListBox or ComboBox with dta from the range row. Having read MS vba.reference documentation I decided to get my range data into an array and use listbox = myarray() and got "Object does not support this method or property." I tried looping through my data range and putting each item in using listbox.additem (mydata()) with the same result. On examination of the listbox prperties AddItem is not there. Seems they have been withdrawn or maybe never existed for Excel VBA.
Any suggestions?
If you are using Additem then you should only add a single item not an array. If you want to use an array you have to use List and the array should be one-dimensional
MyListBox.List=MyOneDArray
Personally I never use .List and an array with Listboxes because I found some circumstances in which it did not work as expected.
There are two ways you can do this:
loop through each item individually and use combobox.additem
Set the combobox.list = array (or variant)
See below for an example of both assuming you want to populate the data from cells A1 to A10 in 2 separate comboboxes:
Private Sub UserForm_Initialize()
Dim i As Integer
Dim arr As Variant
' looping through parameters 1-by-1
With UserForm1.ComboBox1
For i = 1 To 10
.AddItem ThisWorkbook.Sheets("Sheet1").Range("A" & i).Value
Next i
End With
' setting combobox list to arrauy
With UserForm1.ComboBox2
.List = ThisWorkbook.Sheets("Sheet1").Range("A1:A10").Value
End With
End Sub
Make sure to insert your code in the userform code and not the module code

Vba combobox using non-active sheet

I have a combobox to a userform and the its not an activesheet in excel so not sure how to go about it.
The sheet name is "DoNotPrint - Rate Index" and the values within that sheet to appear in the combobox are the columns C2:AS2.
Private Sub ComboBox1_Change()
Sheets("DoNotPrint - Rate Index").Range("C2:AS2") = ComboBox1.Value
End Sub
I tried this code and the combobox list isn't populating those column when the combobox list button is clicked.
The code you posted is for taking what's in the combobox and putting it on the sheet, when it's selected. But if I understand correctly your issue is you can't get the combobox populated. Because you are using a single row as your dataset and not a single column you will need to transpose your data.
Private Sub UserForm_Initialize()
ComboBox1.List = WorksheetFunction.Transpose(Sheet1.Range("C2:AS2"))
End Sub

How can I dynamically fill an Excel combobox with the content of a named table on a different sheet?

on one Excel sheet I have a combobox.
On another sheet, I have a table with a named column ("KontoNr") which should feed into the combobox. The table and column are named in the name manager and are shown as =tabKontenplan and KontoNr =tabKontenplan[KontoNr].
Now I am unsuccessfully trying to fill the comboxbox like this:
combobox.listfillrange = "=tabKontenplan![KontoNr]"
And
combobox.listfillrange = "=KontoNr"
also does not work.
There ist no error, the combobox just remains empty... why is that?
I added an empty ActiveX ListBox control on a worksheet, then typed a couple of random values in a Range on that worksheet, named that range VALUES, and then added this code in the worksheet's code-behind:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ListBox1.ListFillRange = "VALUES"
End Sub
And that's all I needed to do. Remove the = in front of your named range, and it "just works". Make sure the defined name is in scope, and all will go well.
If the named range is scoped to the other worksheet, it won't work. Delete the name and re-create it in workbook scope.
Assuming that "another sheet" is Sheet2,
no good: combobox.listfillrange = "=" & Sheet2.Range("KontoNr").Address
A corrected answer:
combobox.listfillrange = "=Sheet2!" & Sheet2.Range("KontoNr").Address

Populating combobox from named ranges

I have created three dependent drop down lists using excel's validation formula.
The benefit of this is that it is easy to add more options and the dropdown list updates automatically. The structure for this is seen below (where each range is given the same name as the column header).
Is it possible to create the same effect using combo boxes. I can find examples of populating a combo box from hand but not automatically from named ranges
Here is something you can practice with.
Create Combobox1
Populate with a worksheet_selection Change event, the headers range is named "Headers"
The range below the headers are named according to the header names.
Populate combobox2
Change combobox1 to populate combobox2
The Code
Goes into the worksheet module.
Private Sub ComboBox1_Change()
Dim s As String
s = ComboBox1
Me.ComboBox2.List = Range(s).Value
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ComboBox1.List = Application.WorksheetFunction.Transpose(Range("Headers"))
End Sub
i know this is an old post but i found this out by trial and error and wanted to share this with you guys.
when you create a namedrange and want to use that info in your combobox then i have a simple solution here.
for example namedrange is called in your case "Headers"
then do the following
Me.combobox1.RowSource = "Headers"
I don't know why but its adding the info from the named range.

Resources