Results 1 to 6 of 6

Thread: VBA: Set column widths
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Nov 2008
    Posts
    1,016
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default VBA: Set column widths

    I found the post below, but was hoping this could be done a manner where the array is written left-to-right versus up and down.

    As this is it works for my needs after I expand the ar(1) out to ar(15), just hoping it could be shortened.

    I'm starting with column A and setting out to column O with all different column widths.

    Code:
    https://www.mrexcel.com/forum/excel-questions/706877-set-column-widths-array-concept.html
    
    Sub cw() Dim ar(1 To 3) Dim c As Integer Dim cnt As Integer cnt = LBound(ar()) ar(1) = 20 ar(2) = 30 ar(3) = 40 For cnt = LBound(ar()) To UBound(ar()) Columns(cnt).ColumnWidth = ar(cnt) Next cnt End Sub
    Last edited by FryGirl; Aug 21st, 2019 at 06:15 AM. Reason: Grammer

  2. #2
    MrExcel MVP FormR's Avatar
    Join Date
    Aug 2011
    Location
    UK
    Posts
    6,092
    Post Thanks / Like
    Mentioned
    24 Post(s)
    Tagged
    2 Thread(s)

    Default Re: VBA: Set column widths

    Hi, you could try something like this..

    Code:
    Sub cw()
    
    
    Dim c As Variant
    Dim cnt As Integer
    
    
    For Each c In Array(20, 30, 40, 30, 20, 10, 5, 20, 15, 5, 15, 12, 20, 15)
        cnt = cnt + 1
        Columns(cnt).ColumnWidth = c
    Next c
    
    
    End Sub
    [code]your code[/code]

  3. #3
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    75,157
    Post Thanks / Like
    Mentioned
    62 Post(s)
    Tagged
    6 Thread(s)

    Default Re: VBA: Set column widths

    Do you mean like this?
    Code:
    Dim ar As Variant
    Dim cnt As Long
    
    ar = Array(20,30,40)
    
    For cnt = LBound(ar) To UBound(ar)
        Columns(cnt+1).ColumnWidth = ar(cnt)
    Next cnt
    If posting code please use code tags.

  4. #4
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,353
    Post Thanks / Like
    Mentioned
    471 Post(s)
    Tagged
    47 Thread(s)

    Default Re: VBA: Set column widths

    How about
    Code:
    Option Explicit
    Sub FryGirl()
       Dim Ary As Variant
       Dim i As Long
       
       Ary = Array(20, 30, 40)
       For i = 0 To UBound(Ary)
          Columns(i + 1).ColumnWidth = Ary(i)
       Next i
    End Sub
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  5. #5
    Board Regular
    Join Date
    Nov 2008
    Posts
    1,016
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA: Set column widths

    Yes, thank you to all. This is exactly what I was looking for.

  6. #6
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,353
    Post Thanks / Like
    Mentioned
    471 Post(s)
    Tagged
    47 Thread(s)

    Default Re: VBA: Set column widths

    Glad we could help & thanks for the feedback
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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