Auto populate Combobox from range - excel

I am using Excel 2013 and I have written the below code to auto populate a combobox from a range for one of my userforms. I have the combobox named "Times"
Sometimes this populates the list and other times it gives a blank list even though the range did not change.
Any idea how to stop this and just have it work everytime?
Private Sub UserForm_Initialize()
Sheets("Backend").Visible = xlSheetVisible
Times.RowSource = Sheets("Backend").Range("E1:E96").Address
Sheets("Backend").Visible = xlVeryHidden
End Sub

I have found a different way to do this now that works. Thanks to this post.
Populate list box with a named range

Related

Combobox ROWSOURCE from a veryhidden sheet

I have a Combobox in a user form. I get the values for that Combobox from a specific column in my worksheet (Column T).
The Combobox is simply a list of insurance products and their pricing elements, it shows all products on the user form so that the user can select whatever products he wants.
The problem is that the list can grow or decrease (i.e. new products or deleting them). And to make things more complicated, the sheet that has the aforementioned values is located in a very hidden sheet (protected for confidentiality purposes). So whenever I open my userform, the Combobox doesn't show any data (i always have to open the hidden sheet so that they can be shown)
Here's the code
Private Sub UserForm_Initialize()
Products = Range(Range("T7"), Range("T7").End(xlDown)).Address
UserForm11.produit.RowSource = Products
End Sub
Whenever i add the Worksheets name (Worksheets("Calcul").Range(Range("T7"), Range("T7").End(xlDown)).Address) it generates an error.
I just want the combo box to be dynamic and fixed to the source mentioned in the hidden sheet. Any clues?
Private Sub UserForm_Initialize()
dim Products as string
with sheets("Calcul")
Products = .Range("T7", .Range("T7").End(xlDown)).address
end with
UserForm11.produit.RowSource = "'Calcul'!" & Products
End Sub
another way, Products as range.
Private Sub UserForm_Initialize()
dim Products as range
with sheets("Calcul")
Set Products = .Range("T7", .Range("T7").End(xlDown))
end with
UserForm11.produit.list= application.transpose(Products)
End Sub
The WorksheetFunction OFFSET() is made to create dynamic ranges. Creating a Dynamic Named Range in the Name Manager will allows us to see what range is being targeted. See: Dynamic Named Range. The Named Range or its formula can be used as the ComboBox's Rowsource. FYI: the Rowsource updates when the userform loads. You'll have to clear the Rowsource and reassign it to refresh the values after the userform is loaded.
InsuranceProductsList =OFFSET(Calcul!$T$7,1,0,COUNTA(Calcul!$T:$T)-COUNTA(Calcul!$T$1:$T$7),1)

Clearing a list box and any selected items in list

I have an Excel VBA application that goes through a sheet which contains product orders on a sheet in a workbook and searches the worksheet for orders that match various criteria which is populates in a search worksheet. The contents from this worksheet are then displayed in list box. There are several user forms that allow the user to select an order and then manipulate the order. After doing this the order manipulated may not meet the search criteria so I want to clear the list box contents and the selected row in the list box. I have tried numerous things but nothing seems to work. My latest is something list this:
Private Sub ClearListBox()
UserForm5.lstOpenO.ListIndex = -1
UserForm5.lstOpenO.RowSource = ""
End Sub
But I have tried setting the UserForm5.lstOpenO.Selected to false for all the rows. I have tried clearing the search worksheet and then displaying that which should only show the headers on the columns but the highlight in the selected row remains.
Any help would be greatly appreciated
Bruce
First of all you should not use the default instance of the userform in your code. This will lead to ambigous behaviour
I'd suggest
Private Sub ClearSelectionInListBox()
Dim varItm as variant
With lstOpen0
varItm = .MultiSelect
.MultiSelect = 0
.MultiSelect = 1
.MultiSelect = varItm
End With
End Sub
This will clear the selection within the listbox.
It is not clear if you really want to clear the contents. Because if you want then it does not make sense to think about clearing the selection.
If you want to clear the listbox then it is not neccessary to clear the selection first.
Private ClearListBox()
With lstOpen0
.RowSource = ""
.Clear
End With
End Sub
But after that you need to fill the listbox again.
Further reading
VBA userform
Userform.Show
ListBox

How to prepolulate the combobox list from Excel into userform?

I want to take a "list of name" data sets from excel and use it for an Combobox Userform. The user uses a dropdown menu from the Combobox to make his/her selection of a name.
My problem is that I don't know how to apply the "list of name" data sets to the combobox. The range for the list of names is in cell C2:AU2 and its in a worksheet called "DoNotPrint-Names". Once the name is chosen by the user I want it to output it to a cell in "DoNotPrint-Setup" worksheet when the button "Next" is clicked.
Screenshot Part of the Names list:
https://imgur.com/sqsUFmF
Screenshot of Userform:
https://imgur.com/UX8ytrY
I tried the code below which asks the Userform to prepopulate cells from "DoNotPrint - Names" worksheet by transposing it first since its a single row. Not sure how to proceed afterward.
Private Sub UserForm_Initialize()
ComboBox1.List = WorksheetFunction.Transpose(DoNotPrint - Names.Range("C2:AU2"))
End Sub
Select your list and give it name,
Example
Then load that list on your ComboBox1
Code Example
Option Explicit
Private Sub UserForm_Activate()
Dim vList As Variant
For Each vList In [MyList]
Me.ComboBox1.AddItem vList
Next
End Sub
Addendum to found solution
Just in Addition to #0m3r 's valid answer, you can use the array Approach to assign the entire named horizontal list to the comboboxe's .List property in a one liner:
Me.ComboBox1.List = Application.Transpose([myList])
Edit
Alternatively you can use the control's .Column property which seems to be rather unknown:
Me.ComboBox1.Column = [myList].Value

fill multiple excel dropdowns in a loop

I have a for loop which creates multiple dropdowns in different parts of my excel sheet. However i am having a hard time filling these with a list. The list should be obtained from the sheets in ws. I guess the problem here is that, when naming my combo box, it doesnt get assigned as an object.
Here is the code i have. Anybody have an idea on how to do this? Thanks in advance.
Private Sub CommandButton1_Click()
Set ws = Sheets(Array("rpi301", "rpi302", "rpi303", "rpi304", "rpi305"))
For Each Sh In ws
Set Cell = Range(Sh.Range("B8").Value & CStr(Sh.Range("B9").Value))
With Cell
ActiveSheet.DropDowns.Add(.Left, .Top, .Width, .Height).Name = "Combo"
ActiveSheet.Shapes("Combo").ControlFormat.ListFillRange = Sh.Range("B2:B7")
End With
Next
End Sub
Code is right. But the problem what naming the dropdowns. They should have unique names.

Excel Userform Combobox Properties Rowsource box issues?

I have a userform in Excel that works as a calculator.
In this userform I have two ComboBoxs (1 & 2)
In VBA editor, with ComboBox1 selected, In Properties, under Rowsourse I have: Sheet1!a4:a5
In Sheet1, A4 = Auckland and A5 = Christchurch
This is fine and when I run the userform there is a drop down arrow with the two options (Auckland or Christchurch).
However my problem is that when you open this workbook I have a VBA command to hide it from the users sight, leaving them only the userform to work with which is what is desired.
The issue is that if you have another workbook open then open this calculator workbook (which automatically hides itself). Then the combobox list is populated by Sheet1!a4:a5 on the other workbook that was already open, not the workbook that actually contains "Auckland" & "Christchurch" from which the userform is from.
I have tried making the Rowsource for the comboboxes more specific by putting the following in the rowsource box in properties: [book1.xlsm]sheet1!a4:a5 but this comes up with a "Invalid Property Value" error message.
I have also tried making a:
Private Sub Userform1_Initialize()
ComboBox1.Additem "Auckland"
ComboBox1.Additem "Christchurch"
End Sub
And also tried this:
Private Sub Userform1_Initialize()
ComboBox1.RowSource = Workbooks("book1.xlsm").Sheets("Sheet1").Range("a4:a5").Value
End Sub
However with both codes when it opens and runs now the comboboxes are empty and there is no list.
I think the easist solution would be to somehow put the full path (including workbook name) into the rowsource box under properties. But I must be missing something as its coming up with that error for me?
All help would be greatly appreciated.
Thanks
You are missing ' in your full path row source.
It should be like this:
Me.ComboBox1.RowSource = "'[book1.xlsm]Sheet1'!$A$4:$A$5"
I have similar question that can be found HERE.
Set the row source property of the combobox as: SheetName!$Col$Row:$Col$Row, e.g.: Location!$A$1:$A$3.
You may try adding this code on userform:
Private Sub UserForm_Initialize()
ComboBox1.list = Array("Auckland","Christchurch")
End Sub
Then set Combobox propert "MatchEntry" to "1".

Resources