Consulting

Results 1 to 11 of 11

Thread: Solved: Hiding Columns

  1. #1
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location

    Solved: Hiding Columns

    XLD kindly showed me how to hide a column with the following code

    [vba]

    Private Sub ToggleButton1_Click
    If ToggleButton1.Value Then
    Column(8).EntireColumn.Hidden = False
    Else
    Column(8).EntireColumn.Hidden = True
    End If
    End Sub
    [/vba]

    The code does exactly what it was intended to do, so my question here is how does this code change if you wanted to hide more columns?

    Is it a simple Columns(8:10) for consecutive columns, and what about if the columns were not consecutive?

    Also is it a requirement for the False case to precede the True case?

    Ted

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can use the togglebutton value directly
    [VBA]
    Private Sub ToggleButton1_Click()
    Dim c
    For Each c In Array(2, 4, 6, 8)
    Columns(c).EntireColumn.Hidden = ToggleButton1.Value
    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'

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    So we always refer to the columns as their numerical order rather than the Alphaetical listing?

    And as to the question re: false before true? Is this simply a good programming method?

    Ted

  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by Aussiebear
    So we always refer to the columns as their numerical order rather than the Alphaetical listing?
    not neccessarily e.g. [vba]
    Option Explicit

    Private Sub ToggleButton1_Click()
    With Application.Union(Columns("B"), Columns("F"), Columns("H")).EntireColumn
    If ToggleButton1.Value Then
    .Hidden = False
    Else
    .Hidden = True
    End If
    End With
    End Sub

    [/vba]

    Quote Originally Posted by Aussiebear
    And as to the question re: false before true? Is this simply a good programming method?

    Ted
    Doesn't really matter for an If_Then_Else statement
    Last edited by johnske; 09-16-2006 at 08:32 PM. Reason: To simplify
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    As an alternative, you can also use the Not operator, this would generally use a Standard button.
    [vba]
    Private Sub ToggleButton1_Click()
    Dim rg As Range
    Set rg = Union(Columns("B"), Columns("F"), Columns("H"))
    rg.EntireColumn.Hidden = Not (rg.EntireColumn.Hidden)
    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'

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Thank you

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Aussiebear
    XLD kindly showed me how to hide a column with the following code

    [vba]

    Private Sub ToggleButton1_Click
    If ToggleButton1.Value Then
    Column(8).EntireColumn.Hidden = False
    Else
    Column(8).EntireColumn.Hidden = True
    End If
    End Sub
    [/vba]
    I did? That doesn't look like mine, I would (normally) use

    [vba]

    Private Sub ToggleButton1_Click()
    Columns(8).Hidden = Not ToggleButton1.Value
    End Sub
    [/vba]
    that is I would use the logical construct, and I wouldn't have said Entirecolumn when I already identified the column. Must have been a bad day .

    As to colun numbers v letters, I always find numbers easier to work with, and with columns you get the added complexity of AA, etc.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Aussiebear
    And as to the question re: false before true? Is this simply a good programming method?
    I think it is a good programming practice to be consistent, I personally like to code it to take the positive action first.

  9. #9
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Sorry XLD, if I've quoted you incorrectly.

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Aussiebear
    Sorry XLD, if I've quoted you incorrectly.
    You probably haven't, I just wanted to get my excuses in first, I have a reputation to maintain (quiet in the cheap seats)

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by xld
    I did? That doesn't look like mine
    see http://www.vbaexpress.com/forum/showthread.php?t=9351
    I think we all get pulled in to "correct" the OP's code rather than write the "most efficient" method and to be fair, I think it's the right thing to do. He did ask where his error was, not for a new methodology. No harm, of course in showing the alternatives, as has happened here.
    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'

Posting Permissions

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