Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Set Column Width

Change "UsedRange" to "ActiveSheet.UsedRange".


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"tjtjjtjt" wrote in message
...
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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Set Column Width

UsedRange is a property of a WorkSheet (or a ListObject). Try

For Each col In ActiveSheet.UsedRange


In article ,
"tjtjjtjt" wrote:

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.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 51
Default Set Column Width

Thanks, Chip.
--
tj


"Chip Pearson" wrote:

Change "UsedRange" to "ActiveSheet.UsedRange".


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"tjtjjtjt" wrote in message
...
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




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 51
Default Set Column Width

Thanks, JE.


--
tj


"JE McGimpsey" wrote:

UsedRange is a property of a WorkSheet (or a ListObject). Try

For Each col In ActiveSheet.UsedRange


In article ,
"tjtjjtjt" wrote:

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.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Set Column Width

Another thing you may want to change:

Option Explicit
Sub SetColWidth()
Dim col As Range

Cells.EntireColumn.AutoFit

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

If you don't include .columns in activesheet.usedrange.columns, then you're
cycling through all those cells in each column.



tjtjjtjt wrote:

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


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change Cell Width WITHOUT changing Column width dww701 Excel Discussion (Misc queries) 1 January 12th 09 10:52 PM
how do I create multiple column width in the same column in excel Vish Excel Discussion (Misc queries) 9 November 3rd 06 11:49 PM
How to make cell width different than the column width it lies in John Excel Discussion (Misc queries) 2 September 11th 06 10:41 PM
Change the width of a single column in a column chart Dave Charts and Charting in Excel 2 December 13th 04 07:25 PM


All times are GMT +1. The time now is 11:02 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"