View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
tjtjjtjt tjtjjtjt is offline
external usenet poster
 
Posts: 51
Default Set Column Width

I would like to AutoFit all the columns in the UsedRange of a worksheet
unless that would make the column wider than 40. In that case I want to set
the column to 40.

I get an Object Required error 424 message when I attempt the following:

Sub SetColWidth()
Dim col As Range

Cells.EntireColumn.AutoFit

For Each col In UsedRange
If col.ColumnWidth 40 Then
col.ColumnWidth = 40
End If
Next
End Sub


I just want to automate a report that my predecessor did manually. Could
anyone supply me with a brief explanation of the mistake I've made? I'd like
to avoid it in the future.
Thanks in advance for any help.
--
tj