• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Column Width after toggle?

Gregg Wolin

Member
Can someone provide the code required to set the column width of the columns rendered visible?

Code:
For Each c In Rows("1:1").Cells
    If c.Value = "x" Then c.EntireColumn.Hidden = Not c.EntireColumn.Hidden
     'Need code to set column width here? 
    Next c
 
Do you mean something like... below?
Code:
For Each c In Range("A1:D1").Cells
    If c.Value = "x" Then c.EntireColumn.Hidden = Not c.EntireColumn.Hidden
    If Not c.EntireColumn.Hidden Then c.ColumnWidth = 8.43
Next c

Note: You may want to add c.Value = "x" as condition in second if statement as well.
 
Without it, any visible columns will be adjusted to same width. If you want to skip over those column(s) that does not have "x" as value, you'd need that condition.

Syntax:
Code:
If c.Value = "x" And Not c.EntireColumn.Hidden Then c.ColumnWidth = 8.43
 
Back
Top