PC Review


Reply
Thread Tools Rate Thread

Autofit column Help?

 
 
Tim
Guest
Posts: n/a
 
      6th Jan 2010
Hi there,
The code below works great ... BUT ... I need it to apply to only columns G
and H ...PLUS ... I would like to make this across the 25 worksheets I have.
Any ideas?
Thanks very much.
Private Sub Worksheet_Change(ByVal Target As Range)
Me.Application.ActiveWorkbook.RefreshAll
Target.EntireColumn.AutoFit

On Error Resume Next
Target.Dependents.EntireColumn.AutoFit
End Sub
 
Reply With Quote
 
 
 
 
OssieMac
Guest
Posts: n/a
 
      7th Jan 2010
Hi Tim,

Try the following

Private Sub Worksheet_Change(ByVal Target As Range)
Dim isect As Range
Dim ws As Worksheet

Set isect = Application.Intersect _
(Target, Columns("G:H"))
If Not isect Is Nothing Then
Me.Application.ActiveWorkbook.RefreshAll
For Each ws In Worksheets
ws.Columns("G:H").AutoFit
Next ws

'Not sure if you still want the following
On Error Resume Next
Target.Dependents.EntireColumn.AutoFit
End If

End Sub

If you want to exclude any sheets then use the following code in the loop.
You can expand the if statement if more than one sheet to excleude.

For Each ws In Worksheets
If ws.Name <> "MyShtToExclude" Then
ws.Columns("G:H").AutoFit
End If
Next ws


--
Regards,

OssieMac


"Tim" wrote:

> Hi there,
> The code below works great ... BUT ... I need it to apply to only columns G
> and H ...PLUS ... I would like to make this across the 25 worksheets I have.
> Any ideas?
> Thanks very much.
> Private Sub Worksheet_Change(ByVal Target As Range)
> Me.Application.ActiveWorkbook.RefreshAll
> Target.EntireColumn.AutoFit
>
> On Error Resume Next
> Target.Dependents.EntireColumn.AutoFit
> End Sub

 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      7th Jan 2010
Hi again Tim,

Not sure now whether you meant that you wanted all worksheets to have the
auto column fit if the active one was changed or if you only want the active
sheet changed but whatever worksheet is the activesheet.

The following just changes the activesheet (whatever one that might be) but
you need to copy it into ThisWorkbook module.

Private Sub Workbook_SheetChange _
(ByVal Sh As Object, ByVal Target As Range)

Dim isect As Range

Set isect = Application.Intersect _
(Target, Sh.Columns("G:H"))

If Not isect Is Nothing Then
ActiveWorkbook.RefreshAll
Sh.Columns("G:H").AutoFit
'Not sure if you still want the following
On Error Resume Next
Target.Dependents.EntireColumn.AutoFit
End If

End Sub

--
Regards,

OssieMac


"Tim" wrote:

> Hi there,
> The code below works great ... BUT ... I need it to apply to only columns G
> and H ...PLUS ... I would like to make this across the 25 worksheets I have.
> Any ideas?
> Thanks very much.
> Private Sub Worksheet_Change(ByVal Target As Range)
> Me.Application.ActiveWorkbook.RefreshAll
> Target.EntireColumn.AutoFit
>
> On Error Resume Next
> Target.Dependents.EntireColumn.AutoFit
> End Sub

 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      8th Jan 2010
Hi there,
This code works great....I tried your next post, but it kinda of went for a
loop that I could not break. I do want this code to apply to all other
worksheets, but only when they are active. I will copy this code to all
sheets unless, you can think of another way. Thanks for your help.

"OssieMac" wrote:

> Hi Tim,
>
> Try the following
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> Dim isect As Range
> Dim ws As Worksheet
>
> Set isect = Application.Intersect _
> (Target, Columns("G:H"))
> If Not isect Is Nothing Then
> Me.Application.ActiveWorkbook.RefreshAll
> For Each ws In Worksheets
> ws.Columns("G:H").AutoFit
> Next ws
>
> 'Not sure if you still want the following
> On Error Resume Next
> Target.Dependents.EntireColumn.AutoFit
> End If
>
> End Sub
>
> If you want to exclude any sheets then use the following code in the loop.
> You can expand the if statement if more than one sheet to excleude.
>
> For Each ws In Worksheets
> If ws.Name <> "MyShtToExclude" Then
> ws.Columns("G:H").AutoFit
> End If
> Next ws
>
>
> --
> Regards,
>
> OssieMac
>
>
> "Tim" wrote:
>
> > Hi there,
> > The code below works great ... BUT ... I need it to apply to only columns G
> > and H ...PLUS ... I would like to make this across the 25 worksheets I have.
> > Any ideas?
> > Thanks very much.
> > Private Sub Worksheet_Change(ByVal Target As Range)
> > Me.Application.ActiveWorkbook.RefreshAll
> > Target.EntireColumn.AutoFit
> >
> > On Error Resume Next
> > Target.Dependents.EntireColumn.AutoFit
> > End Sub

 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      8th Jan 2010
I may have spoke too soon...I copied the code to other worksheets and it does
not work. Not sure what I am doing wrong? Any ideas?

"OssieMac" wrote:

> Hi Tim,
>
> Try the following
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> Dim isect As Range
> Dim ws As Worksheet
>
> Set isect = Application.Intersect _
> (Target, Columns("G:H"))
> If Not isect Is Nothing Then
> Me.Application.ActiveWorkbook.RefreshAll
> For Each ws In Worksheets
> ws.Columns("G:H").AutoFit
> Next ws
>
> 'Not sure if you still want the following
> On Error Resume Next
> Target.Dependents.EntireColumn.AutoFit
> End If
>
> End Sub
>
> If you want to exclude any sheets then use the following code in the loop.
> You can expand the if statement if more than one sheet to excleude.
>
> For Each ws In Worksheets
> If ws.Name <> "MyShtToExclude" Then
> ws.Columns("G:H").AutoFit
> End If
> Next ws
>
>
> --
> Regards,
>
> OssieMac
>
>
> "Tim" wrote:
>
> > Hi there,
> > The code below works great ... BUT ... I need it to apply to only columns G
> > and H ...PLUS ... I would like to make this across the 25 worksheets I have.
> > Any ideas?
> > Thanks very much.
> > Private Sub Worksheet_Change(ByVal Target As Range)
> > Me.Application.ActiveWorkbook.RefreshAll
> > Target.EntireColumn.AutoFit
> >
> > On Error Resume Next
> > Target.Dependents.EntireColumn.AutoFit
> > End Sub

 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      8th Jan 2010
Hi Tim,

If you use the code from my second post then you must copy it into
ThisWorkbook module; not the Sheet module.

Also you must delete my other code from the Sheet modules.

In case you don't understand what I mean then when you are in the VBA
Editor, on the left hand side there is a column (Window) that is called the
Project Explorer with a list of the modules. (If you can't see this Window
then Select menu item View -> Project Explorer.) In this window you will see
a list of the Sheet modules and also one called ThisWorkbook. Double Click
ThisWorkbook and copy the code from my second post into it.

Double click each of the other sheet modules in turn and delete my other
code from them. (You cannot have any subs called Private Sub
Worksheet_Change(ByVal Target As Range) in the individual sheet modules.)

I have tested the code in my second post and it works on whatever is the
active sheet.

Hope this helps.

--
Regards,

OssieMac


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Column Autofit mcolson Microsoft Excel Misc 2 6th Mar 2008 10:19 PM
Column AutoFit C API Siva Microsoft Excel Programming 1 8th Jan 2008 04:41 PM
how to autofit column widths =?Utf-8?B?YnJldzJ1?= Microsoft Excel New Users 1 31st Oct 2007 11:21 AM
Column Autofit =?Utf-8?B?S2FyZW4gTWNLZW56aWU=?= Microsoft Excel Programming 2 23rd Apr 2007 01:16 PM
Column AutoFit diego Microsoft Excel Misc 0 18th Sep 2003 06:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:47 AM.