select rows from listbox to excel table

kapela2017

New Member
Joined
Oct 16, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
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....

Captura de pantalla excel.png
 
Last edited by a moderator:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi and welcome to MrExcel!

In your explanation you mention table1, but in the code it says tabla2. I leave it as Tabla2. You adjust it if necessary.

VBA Code:
Private Sub CommandButton2_Click()
  Dim tbl As ListObject
  Dim NewRow As ListRow
  Dim i As Long
  
  Set tbl = Sheets("Hoja2").ListObjects("Tabla2")
  With ListBox1
    For i = 0 To .ListCount - 1
      If .Selected(i) Then
        Set NewRow = tbl.ListRows.Add
        NewRow.Range(1) = .List(i, 0)
        NewRow.Range(3) = .List(i, 1)
        NewRow.Range(4) = .List(i, 2)
        NewRow.Range(6) = .List(i, 3)
        NewRow.Range(7) = .List(i, 4)
        .Selected(i) = False    'optional
      End If
    Next
  End With
End Sub

-----
Note Code Tag:
In future please use code tags when posting code.
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.
 
Upvote 0
Solution
Hi and welcome to MrExcel!

In your explanation you mention table1, but in the code it says tabla2. I leave it as Tabla2. You adjust it if necessary.

VBA Code:
Private Sub CommandButton2_Click()
  Dim tbl As ListObject
  Dim NewRow As ListRow
  Dim i As Long
 
  Set tbl = Sheets("Hoja2").ListObjects("Tabla2")
  With ListBox1
    For i = 0 To .ListCount - 1
      If .Selected(i) Then
        Set NewRow = tbl.ListRows.Add
        NewRow.Range(1) = .List(i, 0)
        NewRow.Range(3) = .List(i, 1)
        NewRow.Range(4) = .List(i, 2)
        NewRow.Range(6) = .List(i, 3)
        NewRow.Range(7) = .List(i, 4)
        .Selected(i) = False    'optional
      End If
    Next
  End With
End Sub

-----
Note Code Tag:
In future please use code tags when posting code.
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.
Many, many, many thanks, I'm new to this, he applied it and I'll tell you how it went...
 
Upvote 0
Hi and welcome to MrExcel!

In your explanation you mention table1, but in the code it says tabla2. I leave it as Tabla2. You adjust it if necessary.

VBA Code:
Private Sub CommandButton2_Click()
  Dim tbl As ListObject
  Dim NewRow As ListRow
  Dim i As Long
 
  Set tbl = Sheets("Hoja2").ListObjects("Tabla2")
  With ListBox1
    For i = 0 To .ListCount - 1
      If .Selected(i) Then
        Set NewRow = tbl.ListRows.Add
        NewRow.Range(1) = .List(i, 0)
        NewRow.Range(3) = .List(i, 1)
        NewRow.Range(4) = .List(i, 2)
        NewRow.Range(6) = .List(i, 3)
        NewRow.Range(7) = .List(i, 4)
        .Selected(i) = False    'optional
      End If
    Next
  End With
End Sub

-----
Note Code Tag:
In future please use code tags when posting code.
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.
Many, many, many thanks, I'm new to this, he applied it and I'll tell you how it went...
Many, many, many thanks, I'm new to this, he applied it and I'll tell you how it went...
Sir, I really appreciate your help, honestly, I am very grateful, your code works perfectly, I will study and analyze it, blessings and success
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,945
Members
449,198
Latest member
MhammadishaqKhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top