Consulting

Results 1 to 8 of 8

Thread: Solved: quick question formatting column widths

  1. #1
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location

    Solved: quick question formatting column widths

    This is my code:
    [vba]Columns("P:P").ColumnWidth = 7.86
    Columns("R:R").ColumnWidth = 7.86
    Columns("T:T").ColumnWidth = 7.86
    Columns("V:V").ColumnWidth = 7.86
    Columns("X:X").ColumnWidth = 7.86
    Columns("Z:Z").ColumnWidth = 7.86[/vba]

    Is there a shorter version of doing this to multiple columns that are not contiguous?
    Office 2010, Windows 7
    goal: to learn the most efficient way

  2. #2
    VBAX Contributor
    Joined
    Jul 2004
    Location
    Gurgaon, India
    Posts
    148
    Location
    May be

    [vba]Dim Cols
    Cols = Array(16, 18, 20, 22, 24, 26) 'column numbers
    For c = 0 To UBound(Cols)
    Columns(Cols(c)).ColumnWidth = 7.86
    Next
    [/vba]

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings Tom,

    Range and Union can be used to do such things in one swipe.

    Hope this helps,

    Mark

    [VBA]Sub AdjustColWid()

    Range("P:P,R:R,T:T,V:V,X:X,Z:Z").ColumnWidth = 7.86

    End Sub[/VBA]

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    For regular spacings you can use Mod to identify rows/columns
    [vba]
    Sub CWidth()
    For i = 16 To 26
    If i Mod 2 = 0 Then Columns(i).ColumnWidth = 2
    If i Mod 2 = 1 Then Columns(i).ColumnWidth = 20
    Next
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    MS Excel MVP VBAX Mentor Andy Pope's Avatar
    Joined
    May 2004
    Location
    Essex, England
    Posts
    344
    Location
    this combines all the suggested approaches

    [vba]
    Dim rngCol As Range
    For Each rngCol In Columns("P:Z")
    If rngCol.Column Mod 2 = 0 Then rngCol.ColumnWidth = 7.86
    Next
    [/vba]
    Cheers
    Andy

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    @Malcom:

    Showoff! (Okay, seriously - thank you. WAY more 'adjustable' and I would not have ever thought of that.)

    @Trippy:

    Bright idea for signature!

    Ya'll have a terrific weekend!

    Mark

  7. #7
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    OOPS! Sorry Andy. My typing * laptop speed = late...

    Mark

  8. #8
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    Thanks to all! I knew someone would jump on this very easy question.

    Happy Halloween!
    Office 2010, Windows 7
    goal: to learn the most efficient way

Posting Permissions

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