Re-produce code to copy last row from listbox on userform to sheet

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
427
Office Version
  1. 2016
Platform
  1. Windows
Hi
I have this code
VBA Code:
Private Sub CommandButton1_Click()
Dim i As Long
Dim ary
    Dim ws As Worksheet
   
    Set ws = ThisWorkbook.Worksheets("OUT")
    ReDim ary(0 To 0)
    With Me.ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                ReDim Preserve ary(1 To UBound(ary) + 1)
                ary(UBound(ary)) = .List(i)
            End If
        Next
    End With
   
    ws.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(UBound(ary)).Value _
        = Application.Transpose(ary)
End Sub
should copy selected item from listbox to worksheet. indeed the code just copy the first column for selected item from listbox .
what I want copying the whole lastrow from listbox to row 2 in OUT sheet without select item.
thanks
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
To copy the entire last row from your ListBox to row 2 in the "OUT" sheet without selecting it, you can modify your code as follows:

Excel Formula:
Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Dim lastIndex As Long
    Dim data
    Set ws = ThisWorkbook.Worksheets("OUT")
    
    With Me.ListBox1
        lastIndex = .ListCount - 1
        If lastIndex >= 0 Then
            data = .List(lastIndex)
            ws.Range("A2").Resize(1, UBound(data) + 1).Value = data
        Else
            MsgBox "ListBox is empty."
        End If
    End With
End Sub
 
Upvote 0
it shows mismatch error in this line
VBA Code:
ws.Range("A2").Resize(1, UBound(data) + 1).Value = data
 
Upvote 0
Obviously you can't help After 5 days from the date of the topic despite of I posted after about 5 minutes from your post !

and gave suggest is not logic .

I suggest test it before you reply in the future .

thanks
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,080
Members
453,021
Latest member
Justyna P

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