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
 
well, I thought the first column in listbox takes zero !:oops:
That's correct but solution in link is slicing a 2D array so you start at Column 1
even if I change to 1 still shows error !

just tried it like this & works ok for me

VBA Code:
  Dim c As Long
    With Me.ListBox1
     For c = 1 To 3
        Sheet2.Cells(1, c).Resize(.ListCount).Value = Application.Index(.List, 0, Choose(c, 1, 2, 4))
     Next c
    End With

Dave
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
works ok for me
I'm really shocked!!😵‍💫
mismatch error
VBA Code:
 Sheet2.Cells(1, c).Resize(.ListCount).Value = Application.Index(.List, 0, Choose(c, 1, 2, 4))
 
Upvote 0
I'm really shocked!!😵‍💫
mismatch error
VBA Code:
 Sheet2.Cells(1, c).Resize(.ListCount).Value = Application.Index(.List, 0, Choose(c, 1, 2, 4))
perhaps if can share copy of your workbook (with dummy data) from a file sharing site like dropbox - would help forum to understand why you have the issue

Dave
 
Upvote 0
Try next code:
VBA Code:
    Dim i           As Long
    
    Dim lastrow      As Long
    lastrow = Sheet7.Cells(Rows.Count, 1).End(xlUp).Row + 1

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

    End With
Sheet7 change in the name of your sheet
 
Upvote 0
Try:
Code:
    Dim c           As Long
    Dim r           As Long

    With Me.ListBox1

        For c = 1 To 3

            Dim destCol As Long
            destCol = Choose(c, 1, 2, 3)

            For r = 0 To .ListCount - 1
                sheet2.Cells(r + 1, destCol).Value = .List(r, Choose(c, 0, 1, 3))    ' Map ListBox columns to Sheet2
            Next r

        Next c

    End With
 
Upvote 0
Hi,
issue is way you are populating the listbox & likely to have some null vales which may be causing your error.
Have family with me today but unless another resolves for you, see if can find time to have a further look for you.

BTW as an aside

you can change this

VBA Code:
Private Sub UserForm_Activate()
    Dim MonthName As Variant
    MonthName = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    ComboBox1.List = MonthName
End Sub

to this

VBA Code:
Private Sub UserForm_Activate()
    Me.ComboBox1.List = Application.GetCustomListContents(xlMonth)
End Sub

and if change xlMonth to xlDay you will get weekdays (mon, tue, wed etc)

Dave
 
Upvote 0
thank mike
it works (y)
last code will not copy to the bottom as previous.
will replace data inside sheet and this is not what I want.;)
 
Upvote 0
issue is way you are populating the listbox & likely to have some null vales which may be causing your error.
I'm not sure if I agree with you because the error doesn't occurs about combobox1 .:unsure:
 
Upvote 0

Forum statistics

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