change code copy one column to multipe columns for selected items from list box to sheet

Abdo

Board Regular
Joined
May 16, 2022
Messages
183
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi
I got this code for @Norie
VBA Code:
Private Sub CommandButton1_Click()
Dim I As Long
Dim cnt As Long
Dim arr()

    With ListBox1
        If .ListCount = 0 Then
            MsgBox "No data in listbox."
            Exit Sub
        End If

        For I = 0 To .ListCount - 1
            If .Selected(I) Then
                ReDim Preserve arr(cnt)
                arr(cnt) = .List(I)
                cnt = cnt + 1
            End If

        Next I
    End With

    If cnt = 0 Then
        MsgBox "No data selected."
    Else
        Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(UBound(arr) + 1).Value = Application.Transpose(arr)
    End If

End Sub
it just copies one column from list box into sheet when select specific rows from listbox , but I want copying all of the columns from list box to sheet when select rows from listbox .
the columns in listbox are 8 and the sheet are from A:H
cany anybody help me , please?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hope this helps.

VBA Code:
Private Sub CommandButton1_Click()
Dim i As Long, j As Long
Dim cnt As Long
Dim arr()

    With ListBox1
        If .ListCount = 0 Then
            MsgBox "No data in listbox."
            Exit Sub
        End If

        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                ReDim Preserve arr(7, cnt)
                For j = 0 To 7
                    arr(j, cnt) = .List(i, j)
                Next
                cnt = cnt + 1
            End If
        Next i
    End With

    If cnt = 0 Then
        MsgBox "No data selected."
    Else
        Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(cnt, 8).Value = Application.Transpose(arr)
    End If
End Sub
 
Upvote 0
well done ! is there any way to clear the range in sheet from row2 before copy from listbox?
I mean delete the old data in sheet before copy from listbox .
 
Upvote 0
Is this?
By the way, this code hasn't specified the sheet to paste, is that okay?

VBA Code:
Private Sub CommandButton1_Click()
Dim i As Long, j As Long, cnt As Long
Dim arr()

    With ListBox1
        If .ListCount = 0 Then
            MsgBox "No data in listbox."
            Exit Sub
        End If

        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                ReDim Preserve arr(7, cnt)
                For j = 0 To 7
                    arr(j, cnt) = .List(i, j)
                Next
                cnt = cnt + 1
            End If
        Next i
    End With

    If cnt = 0 Then
        MsgBox "No data selected."
    Else
        Range(Range("A2"), Range("A2").SpecialCells(xlLastCell)).ClearContents
        Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(cnt, 8).Value = Application.Transpose(arr)
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,520
Messages
6,120,008
Members
448,935
Latest member
ijat

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