PDA

View Full Version : insert selected row from listbox to excel table



Kapela2017
10-17-2022, 08:49 PM
Greetings, maybe someone from the community can help me with the following case, I have a listbox named "listbox1" with 5 columns, and I need to insert it into a table named "table1" that has 7 columns, the columns of the listbox do not go from continuously, that is, I need to occupy the columns (1,34,67) of the table, the code shown in the image is the one I am using, but it enters the values ​​contiguously, that is, from column 1 to 5, it also returns all the content of the listbox and I only need it to be the rows that have been selected, I appreciate the help you can provide, blessings....30249

arnelgp
10-17-2022, 09:22 PM
it is confusing when you say your "table1" has 7 columns, but you need to insert the list
in columns (1, 34 and 67).

meaning At least you have 67 columns in your table?

Kapela2017
10-17-2022, 11:39 PM
it is confusing when you say your "table1" has 7 columns, but you need to insert the list
in columns (1, 34 and 67).

meaning At least you have 67 columns in your table?

Greetings, I meant that the data is entered in columns 1,3,4,6,7 of the excel table, so the code must place the listbox columns in the table columns but not continuously

arnelgp
10-18-2022, 12:53 AM
try this code on the click of your button:


Private Sub CommandButton2_Click()
Dim row_data As ListRow
With Me.ListBox1
If .ListIndex <> -1 Then
Set row_data = Sheets("Hoja2").ListObjects("Tabla2").ListRows.Add
row_data.Range(, 1) = .Column(0)
row_data.Range(, 3) = .Column(1)
row_data.Range(, 4) = .Column(2)
row_data.Range(, 6) = .Column(3)
row_data.Range(, 7) = .Column(4)
End If
End With
Set row_data = Nothing
End Sub

macropod
10-18-2022, 12:55 AM
What has this to do with MS Word? You've posted your thread in the Word forum...

macropod
10-18-2022, 01:01 AM
Cross-posted at: select rows from listbox to excel table | MrExcel Message Board (https://www.mrexcel.com/board/threads/select-rows-from-listbox-to-excel-table.1219572/)
Please read VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3