PDA

View Full Version : Solved: Hiding Columns



Aussiebear
09-16-2006, 06:01 PM
XLD kindly showed me how to hide a column with the following code



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


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

mdmackillop
09-16-2006, 06:23 PM
You can use the togglebutton value directly

Private Sub ToggleButton1_Click()
Dim c
For Each c In Array(2, 4, 6, 8)
Columns(c).EntireColumn.Hidden = ToggleButton1.Value
Next
End Sub

Aussiebear
09-16-2006, 06:45 PM
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

johnske
09-16-2006, 06:54 PM
So we always refer to the columns as their numerical order rather than the Alphaetical listing?not neccessarily e.g.
Option Explicit

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




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 :)

mdmackillop
09-16-2006, 07:12 PM
As an alternative, you can also use the Not operator, this would generally use a Standard button.

Private Sub ToggleButton1_Click()
Dim rg As Range
Set rg = Union(Columns("B:D"), Columns("F"), Columns("H"))
rg.EntireColumn.Hidden = Not (rg.EntireColumn.Hidden)
End Sub

Aussiebear
09-17-2006, 02:18 AM
Thank you

Bob Phillips
09-17-2006, 02:49 AM
XLD kindly showed me how to hide a column with the following code



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

I did? That doesn't look like mine, I would (normally) use



Private Sub ToggleButton1_Click()
Columns(8).Hidden = Not ToggleButton1.Value
End Sub

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.

Bob Phillips
09-17-2006, 02:52 AM
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.

Aussiebear
09-17-2006, 02:54 AM
Sorry XLD, if I've quoted you incorrectly.

Bob Phillips
09-17-2006, 03:02 AM
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) :)

mdmackillop
09-17-2006, 05:02 AM
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.:beerchug: