Disable custom menu item based on visible cells

S

stewart

I have a custom excel menu that is paired with a scheduling
worksheet. My worksheet has two sections: the actual schedule A1:O16
and the payroll information P1:T16. I have two menu items one that
will show the payroll information and one that will hide it. I would
like to have just one menu item that will change to Hide or Show
depending on the visibility of the payroll section. Below is the
code. Is it possible?


***Module 1***
Sub AddMenus()
Dim cMenu1 As CommandBarControl
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCutomMenu As CommandBarControl
Dim imenuindex As Integer

On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("&Schedule
Tools").Delete
On Error GoTo 0

Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar")
iHelpMenu = cbMainMenuBar.Controls("Help").Index
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup,
Before:=iHelpMenu)
cbcCutomMenu.Caption = "&Schedule Tools"

With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Show Detail"
.OnAction = "ShowDetail"
End With
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Hide Detail"
.OnAction = "HideDetail"
End With


***Module 2***
Sub ShowDetail()
Application.ScreenUpdating = False
ActiveSheet.Unprotect

Rows("47:48").Select
Selection.EntireRow.Hidden = False
Columns("P:R").Select
Selection.EntireColumn.Hidden = False

'Fit Sheet to Screen
Range("a:s").Select
ActiveWindow.Zoom = True
Range("a2").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True
Application.ScreenUpdating = False
End Sub

Sub HideDetail()
Application.ScreenUpdating = False
ActiveSheet.Unprotect

Rows("47:48").Select
Selection.EntireRow.Hidden = True
Columns("P:R").Select
Selection.EntireColumn.Hidden = True

'Fit Sheet to Screen
Range("a:s").Select
ActiveWindow.Zoom = True
Range("a2").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True
Application.ScreenUpdating = False
End Sub
 
J

Jim Cone

Something along the lines of...
'--
Sub ShowAndHideDetails
If ActionControl.Caption = "Hide" Then
'Code to hide the rows
ActionControl.Caption = "Show"
Else
'Code to show the rows
ActionControl.Caption = "Hide"
End If
End Sub
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)




"stewart"
<[email protected]>
wrote in message
I have a custom excel menu that is paired with a scheduling
worksheet. My worksheet has two sections: the actual schedule A1:O16
and the payroll information P1:T16. I have two menu items one that
will show the payroll information and one that will hide it. I would
like to have just one menu item that will change to Hide or Show
depending on the visibility of the payroll section. Below is the
code. Is it possible?


***Module 1***
Sub AddMenus()
Dim cMenu1 As CommandBarControl
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCutomMenu As CommandBarControl
Dim imenuindex As Integer

On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("&Schedule
Tools").Delete
On Error GoTo 0

Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar")
iHelpMenu = cbMainMenuBar.Controls("Help").Index
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup,
Before:=iHelpMenu)
cbcCutomMenu.Caption = "&Schedule Tools"

With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Show Detail"
.OnAction = "ShowDetail"
End With
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Hide Detail"
.OnAction = "HideDetail"
End With


***Module 2***
Sub ShowDetail()
Application.ScreenUpdating = False
ActiveSheet.Unprotect

Rows("47:48").Select
Selection.EntireRow.Hidden = False
Columns("P:R").Select
Selection.EntireColumn.Hidden = False

'Fit Sheet to Screen
Range("a:s").Select
ActiveWindow.Zoom = True
Range("a2").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True
Application.ScreenUpdating = False
End Sub

Sub HideDetail()
Application.ScreenUpdating = False
ActiveSheet.Unprotect

Rows("47:48").Select
Selection.EntireRow.Hidden = True
Columns("P:R").Select
Selection.EntireColumn.Hidden = True

'Fit Sheet to Screen
Range("a:s").Select
ActiveWindow.Zoom = True
Range("a2").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True
Application.ScreenUpdating = False
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top