PC Review


Reply
Thread Tools Rate Thread

Columns(i).Hidden not changing

 
 
Bryan Loeper
Guest
Posts: n/a
 
      24th Jul 2007
I'm making a toolbar dynamically to hide columns of a workbook. If I
use Columns(i).Hidden = True|False, it works. If I use it in the
below sub (set to the .OnAction of the toolbar button), it doesn't
work correctly, although the sub continues to run past it. The .Tag
nonsense was because it was getting fired twice per button click, so I
added that to keep track of the button's clicked state more
accurately. Any ideas on why my column won't hide?

Sub ButtonToggle()
Dim ctlPressed As CommandBarButton
Dim i As Long
Set ctlPressed = Application.CommandBars.ActionControl
With ctlPressed
i = .index + 1
If .Tag = "0" Then
If .State = msoButtonDown Then
.State = msoButtonUp
Else
.State = msoButtonDown
End If
Columns(i).Hidden = .State
.Tag = "1"
Else
.Tag = "0"
End If
End With
End Sub

 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      24th Jul 2007
Not too sure why you wold be having that problem but have you tried using...

Columns(i).Hidden = CBool(.State)

to coerece the state value to a boolean? I can't really test it without more
of your code but you can give it a try...
--
HTH...

Jim Thomlinson


"Bryan Loeper" wrote:

> I'm making a toolbar dynamically to hide columns of a workbook. If I
> use Columns(i).Hidden = True|False, it works. If I use it in the
> below sub (set to the .OnAction of the toolbar button), it doesn't
> work correctly, although the sub continues to run past it. The .Tag
> nonsense was because it was getting fired twice per button click, so I
> added that to keep track of the button's clicked state more
> accurately. Any ideas on why my column won't hide?
>
> Sub ButtonToggle()
> Dim ctlPressed As CommandBarButton
> Dim i As Long
> Set ctlPressed = Application.CommandBars.ActionControl
> With ctlPressed
> i = .index + 1
> If .Tag = "0" Then
> If .State = msoButtonDown Then
> .State = msoButtonUp
> Else
> .State = msoButtonDown
> End If
> Columns(i).Hidden = .State
> .Tag = "1"
> Else
> .Tag = "0"
> End If
> End With
> End Sub
>
>

 
Reply With Quote
 
Bryan Loeper
Guest
Posts: n/a
 
      24th Jul 2007
Just tried that, no dice. Here's the rest of my code. The above is
in a Module. The following is in ThisWorkbook:

Private Sub Workbook_Open()
ShowHide
End Sub

Sub ShowHide()
Dim cbarName As String
cbarName = "Show/Hide"
Dim cbar As CommandBar
'Delete bar if it exists.
On Error Resume Next
Application.CommandBars(cbarName).Delete
On Error GoTo 0
'Add bar
Set cbar = Application.CommandBars.Add(Name:=cbarName)
Dim i As Long
For i = 2 To 78
Dim Ctrl As CommandBarButton
Set Ctrl = cbar.Controls.Add(msoControlButton)
With Ctrl
.Caption = ActiveSheet.Cells(11, i).Value
.OnAction = "ButtonToggle()"
.Style = msoButtonCaption
.State = Columns(i).EntireColumn.Hidden
.Tag = "0"
End With
Next i
cbar.Visible = True
End Sub

On Jul 24, 12:22 pm, Jim Thomlinson <James_Thomlin...@owfg-Re-Move-
This-.com> wrote:
> Not too sure why you wold be having that problem but have you tried using...
>
> Columns(i).Hidden = CBool(.State)
>
> to coerece the state value to a boolean? I can't really test it without more
> of your code but you can give it a try...
> --
> HTH...
>
> Jim Thomlinson
>
>
>
> "Bryan Loeper" wrote:
> > I'm making a toolbar dynamically to hide columns of a workbook. If I
> > use Columns(i).Hidden = True|False, it works. If I use it in the
> > below sub (set to the .OnAction of the toolbar button), it doesn't
> > work correctly, although the sub continues to run past it. The .Tag
> > nonsense was because it was getting fired twice per button click, so I
> > added that to keep track of the button's clicked state more
> > accurately. Any ideas on why my column won't hide?

>
> > Sub ButtonToggle()
> > Dim ctlPressed As CommandBarButton
> > Dim i As Long
> > Set ctlPressed = Application.CommandBars.ActionControl
> > With ctlPressed
> > i = .index + 1
> > If .Tag = "0" Then
> > If .State = msoButtonDown Then
> > .State = msoButtonUp
> > Else
> > .State = msoButtonDown
> > End If
> > Columns(i).Hidden = .State
> > .Tag = "1"
> > Else
> > .Tag = "0"
> > End If
> > End With
> > End Sub- Hide quoted text -

>
> - Show quoted text -



 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      24th Jul 2007
You're gonna kick yourself. After all of the very nice code you wrote you
none of it ran because your OnAction is wrong...

..OnAction = "ButtonToggle"
not
..OnAction = "ButtonToggle()"
--
HTH...

Jim Thomlinson


"Bryan Loeper" wrote:

> Just tried that, no dice. Here's the rest of my code. The above is
> in a Module. The following is in ThisWorkbook:
>
> Private Sub Workbook_Open()
> ShowHide
> End Sub
>
> Sub ShowHide()
> Dim cbarName As String
> cbarName = "Show/Hide"
> Dim cbar As CommandBar
> 'Delete bar if it exists.
> On Error Resume Next
> Application.CommandBars(cbarName).Delete
> On Error GoTo 0
> 'Add bar
> Set cbar = Application.CommandBars.Add(Name:=cbarName)
> Dim i As Long
> For i = 2 To 78
> Dim Ctrl As CommandBarButton
> Set Ctrl = cbar.Controls.Add(msoControlButton)
> With Ctrl
> .Caption = ActiveSheet.Cells(11, i).Value
> .OnAction = "ButtonToggle()"
> .Style = msoButtonCaption
> .State = Columns(i).EntireColumn.Hidden
> .Tag = "0"
> End With
> Next i
> cbar.Visible = True
> End Sub
>
> On Jul 24, 12:22 pm, Jim Thomlinson <James_Thomlin...@owfg-Re-Move-
> This-.com> wrote:
> > Not too sure why you wold be having that problem but have you tried using...
> >
> > Columns(i).Hidden = CBool(.State)
> >
> > to coerece the state value to a boolean? I can't really test it without more
> > of your code but you can give it a try...
> > --
> > HTH...
> >
> > Jim Thomlinson
> >
> >
> >
> > "Bryan Loeper" wrote:
> > > I'm making a toolbar dynamically to hide columns of a workbook. If I
> > > use Columns(i).Hidden = True|False, it works. If I use it in the
> > > below sub (set to the .OnAction of the toolbar button), it doesn't
> > > work correctly, although the sub continues to run past it. The .Tag
> > > nonsense was because it was getting fired twice per button click, so I
> > > added that to keep track of the button's clicked state more
> > > accurately. Any ideas on why my column won't hide?

> >
> > > Sub ButtonToggle()
> > > Dim ctlPressed As CommandBarButton
> > > Dim i As Long
> > > Set ctlPressed = Application.CommandBars.ActionControl
> > > With ctlPressed
> > > i = .index + 1
> > > If .Tag = "0" Then
> > > If .State = msoButtonDown Then
> > > .State = msoButtonUp
> > > Else
> > > .State = msoButtonDown
> > > End If
> > > Columns(i).Hidden = .State
> > > .Tag = "1"
> > > Else
> > > .Tag = "0"
> > > End If
> > > End With
> > > End Sub- Hide quoted text -

> >
> > - Show quoted text -

>
>
>

 
Reply With Quote
 
Bryan Loeper
Guest
Posts: n/a
 
      24th Jul 2007
:argh:

You're absolutely right. Thanks for the help! It also fixed
that .Tag workaround.


On Jul 24, 1:02 pm, Jim Thomlinson <James_Thomlin...@owfg-Re-Move-
This-.com> wrote:
> You're gonna kick yourself. After all of the very nice code you wrote you
> none of it ran because your OnAction is wrong...
>
> .OnAction = "ButtonToggle"
> not
> .OnAction = "ButtonToggle()"
> --
> HTH...
>
> Jim Thomlinson
>
>
>
> "Bryan Loeper" wrote:
> > Just tried that, no dice. Here's the rest of my code. The above is
> > in a Module. The following is in ThisWorkbook:

>
> > Private Sub Workbook_Open()
> > ShowHide
> > End Sub

>
> > Sub ShowHide()
> > Dim cbarName As String
> > cbarName = "Show/Hide"
> > Dim cbar As CommandBar
> > 'Delete bar if it exists.
> > On Error Resume Next
> > Application.CommandBars(cbarName).Delete
> > On Error GoTo 0
> > 'Add bar
> > Set cbar = Application.CommandBars.Add(Name:=cbarName)
> > Dim i As Long
> > For i = 2 To 78
> > Dim Ctrl As CommandBarButton
> > Set Ctrl = cbar.Controls.Add(msoControlButton)
> > With Ctrl
> > .Caption = ActiveSheet.Cells(11, i).Value
> > .OnAction = "ButtonToggle()"
> > .Style = msoButtonCaption
> > .State = Columns(i).EntireColumn.Hidden
> > .Tag = "0"
> > End With
> > Next i
> > cbar.Visible = True
> > End Sub

>
> > On Jul 24, 12:22 pm, Jim Thomlinson <James_Thomlin...@owfg-Re-Move-
> > This-.com> wrote:
> > > Not too sure why you wold be having that problem but have you tried using...

>
> > > Columns(i).Hidden = CBool(.State)

>
> > > to coerece the state value to a boolean? I can't really test it without more
> > > of your code but you can give it a try...
> > > --
> > > HTH...

>
> > > Jim Thomlinson

>
> > > "Bryan Loeper" wrote:
> > > > I'm making a toolbar dynamically to hide columns of a workbook. If I
> > > > use Columns(i).Hidden = True|False, it works. If I use it in the
> > > > below sub (set to the .OnAction of the toolbar button), it doesn't
> > > > work correctly, although the sub continues to run past it. The .Tag
> > > > nonsense was because it was getting fired twice per button click, so I
> > > > added that to keep track of the button's clicked state more
> > > > accurately. Any ideas on why my column won't hide?

>
> > > > Sub ButtonToggle()
> > > > Dim ctlPressed As CommandBarButton
> > > > Dim i As Long
> > > > Set ctlPressed = Application.CommandBars.ActionControl
> > > > With ctlPressed
> > > > i = .index + 1
> > > > If .Tag = "0" Then
> > > > If .State = msoButtonDown Then
> > > > .State = msoButtonUp
> > > > Else
> > > > .State = msoButtonDown
> > > > End If
> > > > Columns(i).Hidden = .State
> > > > .Tag = "1"
> > > > Else
> > > > .Tag = "0"
> > > > End If
> > > > End With
> > > > End Sub- Hide quoted text -

>
> > > - Show quoted text -- Hide quoted text -

>
> - Show quoted text -



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2007 Worksheet, Hidden Columns, .CSV Format Saves Hidden Column Da Tammy Microsoft Excel Misc 3 2nd Apr 2009 11:40 PM
Copy and Paste with hidden columns remaining hidden Pendelfin Microsoft Excel Misc 2 26th Feb 2009 11:35 AM
Rows hidden by Autofilter vs hidden by changing the Hidden property LEO@KCC Microsoft Excel Programming 4 11th Sep 2007 10:14 AM
Hidden Columns No Longer Hidden after Copying Worksheet? EV Nelson Microsoft Excel Misc 0 6th Dec 2006 04:25 PM
Keeping hidden columns HIDDEN in a copy-and-paste leojbramble Microsoft Excel Discussion 1 21st Oct 2003 05:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:48 AM.