Consulting

Results 1 to 5 of 5

Thread: AutoFit Columns

  1. #1
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    188
    Location

    AutoFit Columns

    Hi

    I have used the macro recorder code below

    [vba]

    Sub AutoFitSheets()
    '
    ' AutoFitSheets Macro
    '


    '
    Sheets("Sheet2").Select

    Columns("A:K").Select

    Selection.Columns.AutoFit

    Sheets("Sheet3").Select

    Columns("A:K").Select

    Selection.Columns.AutoFit

    Sheets("Sheet4").Select

    Columns("A:K").Select

    Selection.Columns.AutoFit

    Sheets("Sheet5").Select

    Columns("A:K").Select

    Selection.Columns.AutoFit

    Sheets("Sheet6").Select

    Columns("A:K").Select
    Selection.Columns.AutoFit
    End
    Sub
    [/vba]

    my question is that I really don't know how many sheets will be in the workbook it could be 2 Sheets to a maximum of 25 Sheets is it possible to just have Autofit run on all sheets within the workbook excluding Sheet1 which is the master sheet for the workbook.

    Regards

    Sooty 8

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]Sub AutoFitAllColsAtoK()
    Dim ws As Worksheet
    For Each ws In Worksheets
    ws.Range("A1:K1").Columns.AutoFit
    Next ws
    End Sub[/VBA]

  3. #3
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    188
    Location
    Hi Kenneth

    Thanks for the reply, I really need to exclude sheet 1 from also running the AutoFit because of the format of that sheet the column width's are set to accomodate more data - is this possible.

    Regards

    Sooty8

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]Sub AutoFitAllColsAtoK()
    Dim ws As Worksheet
    For Each ws In Worksheets
    If Not ws.Name = "Sheet1" Then ws.Range("A1:K1").Columns.AutoFit
    Next ws
    End Sub[/VBA]

  5. #5
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    188
    Location
    Hi Kenneth

    Many thanks works spot on.

    Regards

    Sooty8

Posting Permissions

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