copy specific columns from listbox to userform without selection

Ali M

Active Member
Joined
Oct 10, 2021
Messages
348
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hello ,
I would copy specific columns from listbox to bottom in sheet without selected from listbox.
any idea to fix this code please?


VBA Code:
Dim lastrow As Long
     With Sheet2
        .Range("A" & lastrow + 1).Value = Me.ListBox1.Column(0)
        .Range("B" & lastrow + 1).Value = Me.ListBox1.Column(1)
        .Range("C" & lastrow + 1).Value = Me.ListBox1.Column(3)
    End With
thanks
 
Hi Mike,
can you tell me why start copying from row2 , not row1 ?!
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi @Ali M.
The problem why data copying starts from row 2 and not from row 1 is related to the definition of the lastrow variable in the CommandButton1_Click procedure. In order for the macro to handle cases where the target sheet (Sheet2) has no data (i.e. lastrow = 0), we need to properly initialize lastrow to the first row. Additionally, we need to dynamically update lastrow on each execution to prevent overwriting existing data. Here is the updated code:
VBA Code:
    Dim i           As Long

    With sheet2

        Dim lastrow As Long
        lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row

        If lastrow = 1 And .Cells(1, 1).Value = "" Then
            lastrow = 1
        Else
            lastrow = lastrow + 1
        End If

    End With

    With Me.ListBox1

        For i = 0 To .ListCount - 1
            sheet2.Cells(lastrow, 1).Value = .List(i, 0)
            sheet2.Cells(lastrow, 2).Value = .List(i, 1)
            sheet2.Cells(lastrow, 3).Value = .List(i, 3)
            lastrow = lastrow + 1
        Next i

    End With
 
Upvote 0
Solution

Forum statistics

Threads
1,224,813
Messages
6,181,109
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