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.
Related
I would like to make an autofit to the referenced cells from another sheet, so everytime when I add some infos on one sheet my row height expands, the code below makes that possible:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Target.WrapText = True
Target.EntireRow.AutoFit
End Sub
But as I say this is possible only if I manually typing something anywhere in my workbook, but what about when I have on another sheet (same workbook) some functions that are referencing those values? the fields are not expanding even though it is a right match...
For instance I am having this infos in one sheet (wrapped) as one value:
Column A
AAABBBCCCDDD
EEEFFFGGGHHH
and when using this function on another sheet to make them referenced:
=IFNA(IF(ISBLANK(INDEX(INDEX(Table1[Systembezeichnung];MATCH(Ausdruck!$A$5;Table1[Nummer];0)):INDEX(Table1[Systembezeichnung];MATCH(Ausdruck!$A$5;Table1[Nummer];0)+9);ROW(4:4)));"";INDEX(INDEX(Table1[Systembezeichnung];MATCH(Ausdruck!$A$5;Table1[Nummer];0)):INDEX(Table1[Systembezeichnung];MATCH(Ausdruck!$A$5;Table1[Nummer];0)+9);ROW(4:4)));"")
it gives me everything in one row but not expanding or wrapped as I want:
Column A
AAABBBCCCDDDEEEFFFGGGHHH
It has to be automatically wrapped for the sake of dynamic document that I am providing, without loss of infos or that some infos are missing or "hidden".
You can use Workbook_SheetCalculate event. A simple example will be like below.
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
With Worksheets("Sheet2")
.Range("D2:D5").WrapText = True
.Range("D2:D5").EntireRow.AutoFit
End With
End Sub
So, if you have any formula to range D2:D5 on sheet2 and when you will change any value to any sheet of workbook and if it changes any value to D2:D5 then that cell row will automatically fit.
Edit: For full sheet you can try below codes.
With Worksheets("Sheet2")
.Cells.WrapText = True
.Cells.EntireRow.AutoFit
End With
Microsoft reference here
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.
In my workbook, cells AR8:AS8 are merged and in the cell is a data validation dropdown list. The source of the list uses the formula =indirect(GG8) and this refers to lists in a different tab.
My problem is that when I click on the dropdown, the box isn't wide enough to show the full item.
Is there any way of changing this? I would prefer to NOT use VBA if possible..
I look forward to your responses :)
There is no possible way to achieve this W/O VBA. if you wish to use VBA solution, then please find code below. You have to paste this code to your Worksheet module, not Regular module, and adjust based on comments.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target.Address = "$H$1" Then 'adjust this range to your drop down list
Target.Columns.ColumnWidth = 30 'adjust to your needs
Else
Columns(8).ColumnWidth = 8 'adjust column number to column with drop down values
End If
End Sub
When dropdown is not selected:
Dropdown selected:
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
I am trying to use a range to populate my combobox inside a form I've created. Now I can statically add items using the addItem method, but what if I have tables across various worksheets that contain the description and values - and want to use these as the source for the form.
Any clue?
Dim c
For Each c In Range("A1:A4")
ComboBox1.AddItem c.Value
Next
Hope this helps.