listbox create

I want to make a list box that receives “a1: b10” from the “report” worksheet and shows the two columns in the list box, I’m trying with the code below

    Dim oDialogo3 As Object
   Dim vListBox As Object

Sub AtualizaListBox

Dim oSheet As Object
Dim oRange As Object
Dim i As Integer
Dim s As String

vListBox = oDialogo3.GetControl("ListBox1")

oSheet = ThisComponent.Sheets.getByName("RELATORIO")
oRange = oSheet.getCellRangeByName("A1:B10").RangeAddress

If vListBox.ItemCount > 0 Then vListBox.removeItems(0, vListBox.ItemCount)

For i = 0 To oRange.EndRow - oRange.StartRow
    s = oSheet.getCellByPosition(oRange.StartColumn, oRange.StartRow + i).String
    vListBox.addItem(s, i)
Next i

End Sub

Hello @joaorochaj

I believe a bit of information may help here. Is the listbox on a form or a dialog (don’t see loading dialog or form access)? How is the code run? If you are wanting data from a1:b10 why are you getting cell range a1:a300? Will the list box then result in 20 entries a1-a10 then b1-b20? What is AtualizaListBox2 & AtualizaListBox3?

Hello,

Please see answers in this post → Filling combobox in userform with range in spreadsheet.

Your code is missing some information which you haven’t provided (asked for in comment). The referred to post shows code using a dialog - your code seems to refer to a dialog but neither loads that nor accesses a form if you are not using a dialog.

The referred to post has another answer which, if using a form, displays how code is really unnecessary and can fill directly from the cells.