Multiple non contiguous Column Width Array

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Hi, I am trying to do an array of column widths where specific columns are selected and for each column there is a different width but with no luck, is it actually possible to do this in an array?.

Example

Column B - width 10
Column D - width 19
Column G - width 16
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Is this what you mean?
Code:
Sub ColumnWidth()

    Dim x       As Long
    Dim str     As String
    Dim v       As Variant
    
    str = "B|10,D|19,G|16"
    
    Application.ScreenUpdating = False
    
    For Each v In Split(str, ",")
        Cells(1, Split(v, "|")(0)).ColumnWidth = CLng(Split(v, "|")(1))
    Next v
    
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
Jack, no real problems with your code but a few thoughts.

- str is a function in vba so I would avoid using it as a variable name.
- instead of splitting on commas and then on the vertical bar, I'd consider just a single split then step through 2 at a time
- columnwidth will coerce the string number to numeric so no need to do it specifically

So, a slight variation could be
Code:
Sub ColumnWidth()
    Dim x       As Long
    Dim s     As String
    Dim ary       As Variant
    
    s = "B 10 D 19 G 16"
    ary = Split(s)
    Application.ScreenUpdating = False
    For x = 0 To UBound(ary) Step 2
      Columns(ary(x)).ColumnWidth = ary(x + 1)
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks @Peter_SSs, after I posted the code, I thought I could get rid of the two splits, it was easier to initially conceptualise how to solve the problem and then running with that approach when writing the code out, but like your suggestion; I think also wrote it like that as visually when reading it's easier to identify the column to width pair, but could have also used a 2D array or 2 1D arrays of same size, as well as your suggestion.

Wasn't aware of str being a VBA function, I usually use either msg or strXXX where XXX has a specific term or word depending on the variable use, but noted str is a function too, cheers!
 
Last edited:
Upvote 0
Thanks Jack and Peter Both Work great, Much appreciated
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top