VBA Ecel UserForm multiple selection, dymanic item list

452 views Asked by At

I did userForm with this instruction: http://www.excel-easy.com/vba/examples/multiple-list-box-selections.html

I tried to do dynamic item list based on table,

With ListBox_1
.RowSource = 
 Worksheets("warianty").ListObjects("in_").ListColumns(1).DataBodyRange
.ColumnHeads = True
.ColumnCount = 3
End With

it comes out blank with no list to Select from.I used object table to get dynamic range. Of course I can use other solution, but this one seemed achievable.

Then I want to copy each selected items to other table starting with first row. Here is code i tried to modify based on other button in tutorial.

 Private Sub button_save_Click()
 Dim counter As Integer
 counter = 0
 For i = 0 To ListBox_2.ListCount - 1
 If ListBox_2.Selected(i - counter) Then
 ListBox_2.copy (i - counter) 'it gives error here
Worksheets("dane wejściowe").ListObjects("Tabela41").DataBodyRange(1 + i, 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False,         Transpose:=True
 counter = counter + 1
 End If
 End Sub

besides improving code can SB tell me why I can't just 'copy' list ListBox.value or sth?

1

There are 1 answers

4
Nikolaos Polygenis On BEST ANSWER

Please try the below:

Code

     Private Sub UserForm_Initialize()
  With ListBox_1

        .RowSource = Worksheets("warianty").ListObjects("in_1").ListColumns(1).DataBodyRange.Address
        .ColumnHeads = True
        .ColumnCount = 3

        End With
End Sub


        'end this:

        Private Sub button_save_Click()

        For i = 0 To ListBox_2.ListCount - 1
         If ListBox_2.Selected(i) Then

       Worksheets("dane wejsciowe").Select


        Dim new_row As ListRow
        Set new_row = Worksheets("danewejsciowe").ListObjects("Tabela41").ListRows.Add(AlwaysInsert:=True)




        new_row.Range.Cells(1, 1).Value = ListBox_2.List(i)


         End If


        Next
        End Sub