ListBox List Print data with sort

asyamonique

Well-known Member
Joined
Jan 29, 2008
Messages
1,280
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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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,214,650
Messages
6,120,736
Members
448,988
Latest member
BB_Unlv

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