Command Button code to use with multi-column ListBox

jammoca

Banned
Joined
Nov 6, 2002
Messages
1,100
I have a ListBox ( called ... ListBox_1st_Class ) set up to display 2 columns of data found in .. PrintTemplate!V14:V44

I've set up the ListBox to allow multiple rows to be selected

I have a command button called ... CommandButton_Choose_These_Students ... which when pressed is supposed to send the information from the rows selected in the ListBox to be displayed in a table 2 columns wide starting at PrintTemplate!V49

The code I've entered into the Command Button is ...

Code:
Private Sub CommandButton_Choose_These_Students_Click()
Dim addme As Range
Dim x As Integer
Set addme = Sheet1.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)
For x = 0 To Me.ListBox_1st_Class.ListCount - 1
If Me.ListBox_1st_Class.Selected(x) Then
        addme = Me.ListBox_1st_Class.List(x)
        addme.Offset(0, 1) = Me.ListBox_1st_Class.List(x, 1)
        addme.Offset(0, 2) = Me.ListBox_1st_Class.List(x, 2)
        Set addme = addme.Offset(1, 0)
        End If
    Next x
For x = 0 To Me.ListBox_1st_Class.ListCount - 1
If Me.ListBox_1st_Class.Selected(x) Then Me.ListBox_1st_Class.Selected(x) = False
Next x
End Sub

However, I don't know vba, so didn't create it myself. I made this by amending some code I saw used on a youtube video, so can't figure what the line ... Set addme = Sheet1.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) ... is doing

Is that line instructing where to place the chosen data.

I need the 2-column data from the chosen rows in the listbox to be displayed in a table starting (top left cell) at ... PrintTemplate!V49

Can someone please help me amend this code to do that ?

Kind regards,

Chris
 
You might like to consider the code below, By adding this code to each listbox you can select all the values by just double clicking the listbox, then you could use the original code to place then in your sheet.
NB:- If you double click the Listbox a second time, all the items will become unselected.
NB:-Change listbox name as required.

Code:
Private Sub ListBox_1st_Class_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim x As Long
Static Fd As Boolean
Fd = Fd Xor True
With Me.ListBox_1st_Class
    For x = 0 To .ListCount - 1
        .Selected(x) = Fd
    Next x
End With
End Sub
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Once again, Mick, you've solved my problem. That worked an absolute treat.

I can't thank you enough.

Very, very kindest regards,

Chris
 
Upvote 0

Forum statistics

Threads
1,215,149
Messages
6,123,311
Members
449,095
Latest member
Chestertim

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