Listbox print out how to set column width

Tayl4n

Board Regular
Joined
Feb 17, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
Dear MrExcel community,

I search & fill listbox with textbox then I print listbox values with this code;

VBA Code:
Private Sub cmdPrint_Click()
If ListBox1.ListCount = 0 Then
MsgBox “There is no data for print”
Exit Sub
End If
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim i As Workbook
Set i = Workbooks.Add
Range(“A2”).Resize(ListBox1.ListCount, ListBox1.ColumnCount).Value = ListBox1.List
Unload Me
i.Sheets(1).ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & “\” & “List.pdf”
i.Close False

Listbox codes;
VBA Code:
Private Sub UserForm_Initialize()
ListBox1.ColumnCount = 33
ListBox1.Liste = Range(“A2:AG750”).Value
VBA Code:
But some columns datas not fit. I need to set columns width.
Thank you for your time and interest!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Tayl4n

Board Regular
Joined
Feb 17, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
Dear MrExcel community,

I search & fill listbox with textbox then I print listbox values with this code;

VBA Code:
Private Sub cmdPrint_Click()
If ListBox1.ListCount = 0 Then
MsgBox “There is no data for print”
Exit Sub
End If
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim i As Workbook
Set i = Workbooks.Add
Range(“A2”).Resize(ListBox1.ListCount, ListBox1.ColumnCount).Value = ListBox1.List
Unload Me
i.Sheets(1).ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & “\” & “List.pdf”
i.Close False

Listbox codes;
VBA Code:
Private Sub UserForm_Initialize()
ListBox1.ColumnCount = 33
ListBox1.Liste = Range(“A2:AG750”).Value
VBA Code:
But some columns datas not fit. I need to set columns width.
Thank you for your time and interest!
VBA Code:
‘also searh and filter textbox codes about listbox

Me.ListBox1.Clear
Me.ListBox1.ColumnCount = 33
Me.ListBox1.ColumnHeads = False
Me.ListBox1.ColumnWidths = “”
 

MrIfOnly

Active Member
Joined
Nov 10, 2016
Messages
492
Are you trying to autofit the columns or make them manually adjustable?
 

Tayl4n

Board Regular
Joined
Feb 17, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

Actually
Are you trying to autofit the columns or make them manually adjustable?
Hey actually I manually setting with these codes but datas changing so autofit would be much better solution for me 🙏🏻❤️

VBA Code:
i.Sheets(1).Range("D:D").columnwidth = 20  'change to suit
 

MrIfOnly

Active Member
Joined
Nov 10, 2016
Messages
492
Untested, but how about:

VBA Code:
With i.Sheets(1)
        With .PageSetup
             .Zoom = False 
             .FitToPagesTall = 1 
             .FitToPagesWide = 1
       End With
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & “\” & “List.pdf”
End With

Not sure if that'll work for PDF or not.
 

Tayl4n

Board Regular
Joined
Feb 17, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
Untested, but how about:

VBA Code:
With i.Sheets(1)
        With .PageSetup
             .Zoom = False
             .FitToPagesTall = 1
             .FitToPagesWide = 1
       End With
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & “\” & “List.pdf”
End With

Not sure if that'll work for PDF or not.
Unfortunately not work :(
 

MrIfOnly

Active Member
Joined
Nov 10, 2016
Messages
492
Shame. I, unfortunately, don't have any other suggestions for you.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,790
Messages
5,638,322
Members
417,020
Latest member
MSVII

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