Macro to set specific column width for columns with data

samc22

New Member
Joined
May 17, 2011
Messages
1
Hi,

I'm building an excel model where users may enter data by column along a time scale with projects by row.

I'd like to summarise the table for printing by compacting the columns without data to the width of the header (without hiding them), while setting the width of the rows with data to a predefined width.

p.s. I'm not the greatest with code so any help would be awesome!

Thanks very much,

Scott
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi Scott,

Maybe something like this:

Code:
Sub AutosizeWorksheet()
Dim Cl As Long, Rw As Long, LastCl As Long, LastRw As Long
LastCl = ActiveSheet.UsedRange.Columns.Count
LastRw = ActiveSheet.UsedRange.Rows.Count
For Cl = 1 To LastCl
    If Cells(LastRw, Cl) = "" Then
        Columns(Cl).AutoFit
    Else: Columns(Cl).ColumnWidth = 30
    End If
Next Cl
End Sub

You can change the number 30 to whatever you want the new width to be.

abousetta
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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