0

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 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, the button works great! Unfortunately, item list is still blank, although I found another Dim lr As Long lr = Worksheets("warianty").ListObjects("in_").Range.Rows.Count With ListBox1 For i = 1 To lr .AddItem Worksheets("warianty").ListObjects("in_").DataBodyRange(i, 1).Value Next i End With
it works, you just add after DataBodyRange .Address.I just run it and run with no issue.Please vote my answer
Your code worked for me once when I used F8, then when I pushed F5 it went blank again. It gets proper number of row, but they are blank
Please use this: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.