Filling combobox in userform with range in spreadsheet

Hello
I have a userform with a combobox
I would like to fill this combobox
with data from range of spreadshet,

is it possible?

Thank you

LO: 5.2
Windows: 7 e 10

If the combo box is in a Dialog or Userform then you need to populate the list for the combobox in the macro that initialises the Dialog.

Simple example below expects the range to initialise the combo box list to be in a vertical column range of cells.

Sub StartDialog

    Dim oList As Object
    Dim n As Integer
    Dim i As Integer
    Dim oDialog As Variant
    Dim oData As Variant
	
    ' Code for initiating and showing the dialog	'	
    DialogLibraries.LoadLibrary("Standard")
    oDialog = CreateUnoDialog( DialogLibraries.Standard.ImportTextFormat )
    
    oList = oDialog.getControl("FormatList")
    
    oList.Text = "Select text input format from the list."
    
    ' Read the data list from cell range into a variant array '
    oData = ThisComponent.Sheets(0).getCellRangeByName("C7:C9").DataArray
    
    n = ubound(oData)
    
    For i = 0 to n
        oList.addItem(oData(i)(0) ,i)
    Next i
    
    oDialog.Execute()
End Sub

Thank you Mark :smiley:

Hi

Yes, with Combo Box and ListBox you can define the Source cell range in the Data tab (design mode):

Note: as you can see in the screenshot linked cell (B1 in this example) is a text, even if the source data range contains numbers.

[EDIT]

I join the Combo.ods example so you can check the form.

Regards

I’m sorry Pierre,
but in your example the combobox is not in a userform

The “Data” Tab is not in useform combobox

@Iniziato - When you create a combo box or a drop-down list, or whatever the type of control, a form is always created to which these controls are attached. To verify this, display the Form Design toolbar and use Form Navigator tool. That said, I may not have understood your question, but in this case you should give more precision on your form.

Regards