+ Reply to Thread
Results 1 to 5 of 5

Simple autofit macro crashes.

  1. #1
    Registered User
    Join Date
    09-15-2006
    Posts
    31

    Simple autofit macro crashes.

    I just want to autofit 2 sheets but I get:
    Run-time error '1004':
    Select method of range class failed

    Workbooks(antalwb + 1).Sheets(1).Cells.Select
    Workbooks(antalwb + 1).Sheets(1).Cells.EntireColumn.AutoFit
    Workbooks(antalwb + 1).Sheets(1).Range("A1").Select

    Workbooks(antalwb + 1).Sheets(2).Cells.Select <--- Crashes here
    Workbooks(antalwb + 1).Sheets(2).Cells.EntireColumn.AutoFit
    Workbooks(antalwb + 1).Sheets(2).Range("A1").Select

    First section works but not the second.
    help!

  2. #2
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    Are your worksheets hidden? if they are you first need to make them visible. Try posting your entire code for a more detailed response.

    Regards,
    Simon

  3. #3
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    hi Peter,

    Assuming your sheets are visible as suggested by Simon, are you sure that you actually have a "sheets(2)"?
    eg the original may have been deleted & now be "sheets(3)" - this can be checked by looking at the sheets under the Project Explorer section of the VB Editor.

    btw, I'm not sure if this will help at all but the code could by tidied/shortened to:

    with Workbooks(antalwb + 1).Sheets(1)
    .Cells.EntireColumn.AutoFit
    .Range("A1").Select
    end with

    I'm not 100% on this but I think that a sheet needs to be active before you try & select the "cells" on it so I've changed your second section of code to:

    with Workbooks(antalwb + 1).Sheets(2)
    .Select <--- Crashes here
    .Cells.EntireColumn.AutoFit
    .Range("A1").Select
    end with

    hth
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  4. #4
    Registered User
    Join Date
    09-15-2006
    Posts
    31
    I searched and found this piece of code which solved everything

    Dim wkSt As String
    Dim wkBk As Worksheet
    wkSt = ActiveSheet.Name
    For Each wkBk In ActiveWorkbook.Worksheets
    On Error Resume Next
    wkBk.Activate
    Cells.EntireColumn.AutoFit
    Next wkBk
    Sheets(wkSt).Select

  5. #5
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    Peter, be aware that using "On Error Resume Next" is not the preffered way to handle errors because it ignores all errors so you will be unaware if your code runs or not! try a different method of trapping and handling errors.

    Regards,
    Simon

+ 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