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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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,216,095
Messages
6,128,795
Members
449,468
Latest member
AGreen17

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