+ Reply to Thread
Results 1 to 6 of 6

AutoFit Columns in specific worksheet

  1. #1
    Registered User
    Join Date
    02-05-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    3

    AutoFit Columns in specific worksheet

    Hi,

    can anyone help me alter this macro so that it works in specific worksheet, not for the whole work book.

    Please Login or Register  to view this content.
    The macro works fine, but i would like it to not affect all the active sheets.

    many thanks!
    Last edited by Cutter; 07-16-2012 at 10:21 AM. Reason: Added code tags

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: AutoFit Columns in specific worksheet

    Why do you need to loop through all the cells in the range?

    Just use the following in the Worksheet's Change event not the Workbook Change

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    02-05-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: AutoFit Columns in specific worksheet

    Thanks...

    tried...comes up with the following error..

    Compile error:Ambiguous name detected: Worksheet_Change

    i want any the columns to autofit as data is entered into the cells in that column, but there are hidden columns that i want to remain hidden
    the original code works fine, but i have a summary sheet at the start which i dont want to alter

    hope this is clearer...

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: AutoFit Columns in specific worksheet

    Did you put the line of code
    Target.EntireColumn.AutoFit

    inside the Sheet Change event procedure that you see in the VBE when you select the sheet. I suspect you've just added the code I gave you as a module level procedure, or perhaps added it manually in the sheet code area.

    Given your addendum, rather than just use Target.EntireColumn......then just use
    Cells.EntireColumn.Autofit

    and then some code to hide the columns you don't want to see. e.g.

    Range("E:E").ColumnWidth = 0

  5. #5
    Registered User
    Join Date
    02-05-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: AutoFit Columns in specific worksheet

    Thanks Richard,
    i have managed to merge your solution with my original one and have found a suitable, if not entirely perfect outcome.

    Please Login or Register  to view this content.
    Thanks again for your help....
    Last edited by Cutter; 07-16-2012 at 10:21 AM. Reason: Added code tags

  6. #6
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: AutoFit Columns in specific worksheet

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1