ListBox List Print data with sort

asyamonique

Well-known Member
Joined
Jan 29, 2008
Messages
1,286
Office Version
  1. 2013
Platform
  1. Windows
Code:
Dim ws As WorksheetDim rng As Range
Dim i As Long
Dim J As Long


Set ws = Worksheets.Add
Set rng = ws.Range("A1")
For i = 0 To ListBox1.ColumnCount - 1
For J = 0 To ListBox1.ListCount - 1
rng.Offset(J, i) = ListBox1.Column(i, J)
Next J
Next i
Columns("A:A").Resize(, ListBox1.ColumnCount).EntireColumn.AutoFit
Application.DisplayAlerts = False
ws.PageSetup.Orientation = xlLandscape
ws.PrintOut
ws.Delete
Application.DisplayAlerts = True

Good Day,
My question with that given above code is there any possibility to sort listbox column 4 datas from A to Z?
Many Thaks.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi,
See if this suits:
Rich (BB code):
Sub Test()
 
  Dim ws As Worksheet
  Dim rng As Range
 
  ' Turn blinking off
  Application.ScreenUpdating = False
 
  ' Provide all actions in new aux workbook
  With Workbooks.Add(xlWBATWorksheet)
   
    Set ws = .Sheets(1)
    Set rng = ws.Range("A1").Resize(ListBox1.ListCount, ListBox1.ColumnCount)
   
    With rng
     
      ' Copy data from ListBox1 to the range
      .Value = ListBox1.List
     
      ' Sort the range by the 4d column
      .Sort .Cells(1, 4), xlAscending, Header:=xlNo
     
      ' Copy the sorted data back to the ListBox1 (if it's required)
      ListBox1.List = .Value
     
    End With
   
    ' Print the sorted data
    ws.PrintOut
   
    ' Close the aux workbook
    .Close False
   
  End With
 
  ' Restore screen updating
  Application.ScreenUpdating = True
 
End Sub
Regards
 
Last edited:
Upvote 0
Hi,
Thanks for the code it works good, is it possible to autofit the columns?
Or choosing as column number like only the columns 1-3-4-5
Many Thanks
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,707
Members
452,667
Latest member
vanessavalentino83

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