Consulting

Results 1 to 5 of 5

Thread: macro help

  1. #1
    VBAX Newbie
    Joined
    Aug 2011
    Posts
    2
    Location

    macro help

    hi there, im currently trying to make a macro to start automatically when the a file is opened, its just really to change the default column width in excel. but when i try and run it i just get errors all the time. I'm using excel 2007. My code is


    [VBA]Sub Auto_Open()
    '
    ' Auto_Open Macro
    '
    Columns("A:Z").select
    Range("Z1").Activate
    selection.coulmnwidth=20
    with selection
    .horizontalAlignment=x1Left
    .VerticalAlignment= x1Bottom
    .Orientation=0
    .WrapText=False
    .IndentLevel=0
    .ShrinkToFit=False
    .ReadingOrder=xlContext
    .MergeCells=False
    end with
    activeWindow.LargeScroll ToRight:=-1
    Range("K1").select
    activeWindow.smallScroll Down:=-15
    End Sub[/VBA]


    And the Error is "Method 'Columns' of object '_Global' failed"

    if anyone could help thatd be great thanks , Jim
    Last edited by Bob Phillips; 08-10-2011 at 05:45 AM.

  2. #2
    VBAX Mentor
    Joined
    Apr 2009
    Location
    Kingsbury
    Posts
    423
    Location
    g3

    This should work

    Put the routine in the Workbook open event
    [VBA]
    Option Explicit
    Private Sub Workbook_Open()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")
    With ws
    .Range("A1").ColumnWidth = 20
    End With
    End Sub
    [/VBA]

    Rob

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    This should work

    [vba]

    Sub Auto_Open()
    With Worksheets(1)

    .Activate

    With .Columns("A:Z")

    .ColumnWidth = 20
    .HorizontalAlignment = x1Left
    .VerticalAlignment = x1Bottom
    .Orientation = 0
    .WrapText = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
    End With

    .Range("K1").Select
    End With
    End SUb[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Newbie
    Joined
    Aug 2011
    Posts
    2
    Location
    thanks very much lads for the speedy replys, they both worked, i dont trust that macro record thing now

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Its not a matter of not trusting it, but more about when to adjust what its tellling you
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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