0
votes

I have a listbox named listBox1 on a user form in Excel VBA and a button named submit also on the form. The listbox is populated from a dynamic range starting on cell A2 of sheet 2. I want to export the contents of this listbox to a named range named dataCells on sheet 1. The code I am using currently is close but somehow exports the listbox data to cell A1 of sheet 1 instead of starting in the first cell of the named range "data cells". What am I doing wrong?

//Code to populate listBox 1

Private Sub Userform1_initialize()
    Dim dataItems as Range
    Dim item as Range

    worksheets("sheet2").Activate
    Set dataItems = Range("A2" , Range("A2").end(xlDown))
    for each item in dataItems
        listbox1.addItem(item)
    Next item
End sub

//Code to export the listbox contents to named range in sheet 1

Private Sub Submit_Click()

    Dim dataCells as Range
    Dim dataCount as Integer
    Dim i as integer

    worksheets("sheet1").Activate
    dataCount = listBox1.ListCount - 1
    set dataCells = Range("B2" , Range("B2").offset(0, dataCount))

    for i = 0 to listBox1.ListCount - 1
        dataCells(0, i) = listBox1.list(i , 0) // exports to A1 of sheet 1??
    next i
End sub
2
dataCells will be a one-based 2-D array, not zero-based. In the immediate pane in the VBE ? Range("B2").Cells(0,0).Address() gives "$A$1"Tim Williams

2 Answers

0
votes

Give this a try and let me know whether it works for you. Note that if the items are not strings, you can change dataArray to a Variant (if you are using VB). Basically, I placed the listbox items into an array and then stuffed it into a Range:

Private Sub Submit_Click()

    worksheets("sheet1").Activate

    Dim i as integer
    dim dataArray(listBox1.ListCount-1) as String
    for i = 0 to listBox1.ListCount - 1
        dataArray(i) = listBox1.list(i , 0) 
    next i

    Range("B2").Resize(listBox1.ListCount -1,1) = dataArray

End sub
0
votes

in my code example i want to show a fast way to populate the listbox without using additem,

also fo exporting to sheet1, i used a VBA Array but listbox1.list also works (i added comments)

this works :

Option Explicit

Private Sub UserForm_Activate()
Dim i&
Dim dataItems As Range
With Me
    With .ListBox1
        .Clear 'not needed in userform_initialize, but i did it in a _activate sub

        With Worksheets("sheet2")
            Set dataItems = .Range("A2", .Cells(.Rows.Count, 1).End(xlUp)) 'i modified this because if your code hits a blank, it will think its the last line...
        End With

        .List = dataItems.Value

        Dim Data()
        ReDim Data(1 To .ListCount, 1 To 1)
        Data = .List

        'this section goes to Submit_Click()
        With Worksheets("sheet1")
            Set dataItems = .Range("B2", .Range("B2").Offset(Me.ListBox1.ListCount - 1, 0))
        End With
        With dataItems
            .Value = Data '.value2=me.listbox1.list  , works too
        End With
    End With 'listbox1
End With 'me
End Sub