Selected ListBox Items to sheet

johnster

New Member
Joined
May 31, 2018
Messages
43
I have ListBox1 with multiple columns and rows. What i wanna do is when one item is clicked or highlighted copy the row to different cells in a sheet (name sheet is "Factuurvoorbeeld").
After that i can do a printpreview to print.

This is my code so far:

Private Sub CommandButton3_Afdrukvoorbeeld_Click()
With ListBox1
For i = 1 To ListBox1.ListCount - 1
If .Selected(i) = True Then
Sheets("Factuurvoorbeeld").Cells(18, 5) = ListBox1.List(i, 0)
'ActiveWorkbook.Sheets("Factuurvoorbeeld").Cells(17, 5) = ListBox1.List(i, 1)
'ActiveWorkbook.Sheets("Factuurvoorbeeld").Cells(19, 5) = ListBox1.List(i, 2)
'ActiveWorkbook.Sheets("Factuurvoorbeeld").Cells(19, 9) = ListBox1.List(i, 3)
'ActiveWorkbook.Sheets("Factuurvoorbeeld").Cells(20, 5) = ListBox1.List(i, 4)
'ActiveWorkbook.Sheets("Factuurvoorbeeld").Cells(20, 6) = ListBox1.List(i, 5)
'ActiveWorkbook.Sheets("Factuurvoorbeeld").Cells(37, 4) = ListBox1.List(i, 6)
'ActiveWorkbook.Sheets("Factuurvoorbeeld").Cells(25, 7) = ListBox1.List(i, 7)
'ActiveWorkbook.Sheets("Factuurvoorbeeld").Cells(32, 4) = ListBox1.List(i, 8)
End If
Next i
UserForm1.Hide
ActiveWorkbook.Sheets("Factuurvoorbeeld").PrintPreview
UserForm1.Show
End With
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

MrIfOnly

Active Member
Joined
Nov 10, 2016
Messages
492
You don't explain the problem you're having. What you have should do what you want. But if you are using the fmMultiSelectSingle property for the listbox you can do away with the loop like so:

Code:
[COLOR=#333333]Private Sub CommandButton3_Afdrukvoorbeeld_Click()[/COLOR]
[COLOR=#333333]With ListBox1[/COLOR]
[COLOR=#333333]i =[/COLOR] .ListIndex

With Sheets("Factuurvoorbeeld")
    .Cells(18, 5) = .List(i, 0)
    .Cells(17, 5) = .List(i, 1)
    .Cells(19, 5) = .List(i, 2)
    .Cells(19, 9) = .List(i, 3)
    .Cells(20, 5) = .List(i, 4)
    .Cells(20, 6) = .List(i, 5)
    .Cells(37, 4) = .List(i, 6)
    .Cells(25, 7) = .List(i, 7)
    .Cells(32, 4) = .List(i, 8)
End With
[COLOR=#333333]UserForm1.Hide[/COLOR]
[COLOR=#333333]ActiveWorkbook.Sheets("Factuurvoorbeeld").PrintPreview[/COLOR]
[COLOR=#333333]UserForm1.Show[/COLOR]
[COLOR=#333333]End With[/COLOR]
[COLOR=#333333]End Sub[/COLOR]

Regards,

CJ
 
Last edited:

johnster

New Member
Joined
May 31, 2018
Messages
43
Thx for your answer, This is working.

Now I want to delete the cells in the sheet "Factuurvoorbeeld" after closing or printing the printpreview.
When i place the code (Call sbClearCells) after your code it's not working.

Sub sbClearCells()
ActiveWorkbook.Sheets("Factuurvoorbeeld").Range("E17").Clear
ActiveWorkbook.Sheets("Factuurvoorbeeld").Range("E18").Clear
ActiveWorkbook.Sheets("Factuurvoorbeeld").Range("E19").Clear
ActiveWorkbook.Sheets("Factuurvoorbeeld").Range("I19").Clear
ActiveWorkbook.Sheets("Factuurvoorbeeld").Range("E20").Clear
ActiveWorkbook.Sheets("Factuurvoorbeeld").Range("F20").Clear
ActiveWorkbook.Sheets("Factuurvoorbeeld").Range("G25").Clear
ActiveWorkbook.Sheets("Factuurvoorbeeld").Range("D32").Clear
ActiveWorkbook.Sheets("Factuurvoorbeeld").Range("D37").Clear
End Sub
 

MrIfOnly

Active Member
Joined
Nov 10, 2016
Messages
492
What you have there should have worked as long as sbClearCells is located in a standard module and not the userform module. You can also use the With...End With statements to clean it up a bit:

Code:
Sub sbClearCells()
With ActiveWorkbook.Sheets("Factuurvoorbeeld")
    .Range("E17").Clear
    .Range("E18").Clear
    .Range("E19").Clear
    .Range("I19").Clear
    .Range("E20").Clear
    .Range("F20").Clear
    .Range("G25").Clear
    .Range("D32").Clear
    .Range("D37").Clear
End With
End Sub

Regards,

CJ
 

Forum statistics

Threads
1,136,328
Messages
5,675,132
Members
419,551
Latest member
thangxpm

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
Top