Advertisement

10.31.2007 at 07:32PM PDT, ID: 22931490
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Using customized context menu in Excel: approach
Tags: menu, new, popup, context, using
Fellow Experts,

I am developing an Excel application in which I want to preempt people from using the built-in
Cell, Row, and Column context (popup) menus, and instead use a context menu I build.  (Or,
if it is easier, to simply rebuild those three CommandBars so that they show what I want,
and then reset them when needed.)

The following guidelines should be considered:

1) I would only want this behavior to occur on certain worksheets in a specified workbook.

2) Other worksheets within that workbook, and other open workbooks, should show the
normal right-click behavior.

At this juncture I am looking for advice on the approach.  The first hurdle is: should I attempt
(and is it even possible) to force a different context menu to come up, or should I just recast
the Cell, Row, and Column menus to fit my needs?

Second hurdle: what events should I rely on?  To prevent having to put redundant code in the
the sheet modules, I figured I would use workbook events.  I will need to be able to accommodate
users moving between worksheets in this workbook, and between workbooks.

Useful guidance shall be much appreciated.  As the approach gets more defined, I may create
new questions to handle specific coding details.

Regards,

Patrick
Start your free trial to view this solution
Question Stats
Zone: Software
Question Asked By: matthewspatrick
Solution Provided By: webtubbs
Participating Experts: 3
Solution Grade: A
Views: 5
Translate:
Loading Advertisement...
10.31.2007 at 11:22PM PDT, ID: 20191064

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
10.31.2007 at 11:34PM PDT, ID: 20191091

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.01.2007 at 05:47AM PDT, ID: 20192305

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.01.2007 at 12:48PM PDT, ID: 20196060

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.02.2007 at 04:00PM PDT, ID: 20205042

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.02.2007 at 04:39PM PDT, ID: 20205205

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.03.2007 at 07:43AM PDT, ID: 20206984

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.03.2007 at 07:54AM PDT, ID: 20207033

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.03.2007 at 07:57AM PDT, ID: 20207056

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
10.31.2007 at 11:22PM PDT, ID: 20191064
The ThisWorkbook refers to the whole workbook. Would be better create your menus to each sheet specifically?
Use the Worksheet_SelectionChange event to switch between menus

 
10.31.2007 at 11:34PM PDT, ID: 20191091

Rank: Genius

Hi Patrick,

My example below clears the relevant menus of the original controls and replaces them with the new ones. Because the CommandBar object contains a handy Reset method, I use that to revert back to the original controls.

All the below code goes in the ThisWorkbook module....

'//////////////////////////
'//// CODE START ////
'/////////////////////////
Option Explicit

Dim CustomMenuSheets As Object
Dim CellMenu As CommandBar
Dim RowMenu As CommandBar
Dim ColumnMenu As CommandBar

Private Sub Workbook_Open()
    Set CustomMenuSheets = CreateObject("Scripting.Dictionary")
    With CustomMenuSheets
        .Add "Sheet1", "Sheet1"
        .Add "Sheet2", "Sheet2"
    End With
    Set CellMenu = Application.CommandBars("Cell")
    Set RowMenu = Application.CommandBars("Row")
    Set ColumnMenu = Application.CommandBars("Column")
End Sub

Private Sub Workbook_Activate()
    If CustomMenuSheets.exists(ActiveSheet.Name) Then
        NewCellMenu
        NewRowMenu
        NewColumnMenu
    Else
       CellMenu.Reset
       RowMenu.Reset
       ColumnMenu.Reset
    End If
End Sub

Private Sub Workbook_Deactivate()
       CellMenu.Reset
       RowMenu.Reset
       ColumnMenu.Reset
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    If CustomMenuSheets.exists(Sh.Name) Then
        NewCellMenu
        NewRowMenu
        NewColumnMenu
    Else
       CellMenu.Reset
       RowMenu.Reset
       ColumnMenu.Reset
    End If
End Sub

Public Sub NewCellMenu()
    Dim i As Long

    'clear the controls from the menu
    Dim cb As CommandBarControl
    For Each cb In CellMenu.Controls
    cb.Delete
    Next

    'add your new controls
    Dim cb1 As CommandBarButton
    Set cb1 = CellMenu.Controls.Add(msoControlButton)
    With cb1
        .Caption = "Control1"
        .OnAction = "Macro1"
        .Style = msoButtonCaption
    End With

    Dim cb2 As CommandBarButton
    Set cb2 = CellMenu.Controls.Add(msoControlButton)
    With cb2
        .Caption = "Control2"
        .OnAction = "Macro2"
        .Style = msoButtonCaption
    End With

    Dim cb3 As CommandBarButton
    Set cb3 = CellMenu.Controls.Add(msoControlButton)
    With cb3
        .Caption = "Control3"
        .OnAction = "Macro3"
        .Style = msoButtonCaption
    End With

    Dim cb4 As CommandBarButton
    Set cb4 = CellMenu.Controls.Add(msoControlButton)
    With cb4
        .Caption = "Control4"
        .OnAction = "Macro4"
        .Style = msoButtonCaption
    End With

    Dim cb5 As CommandBarButton
    Set cb5 = CellMenu.Controls.Add(msoControlButton)
    With cb5
        .Caption = "Control5"
        .OnAction = "Macro5"
        .Style = msoButtonCaption
    End With

End Sub

'//////////////////////////
'////// CODE END//////
'/////////////////////////

Regards,

Wayne
Accepted Solution
 
11.01.2007 at 05:47AM PDT, ID: 20192305
Wayne: Thanks!  This just may preempt my follow-ups.  You are da bomb :)

macsek wrote:
> The ThisWorkbook refers to the whole workbook.

I am well aware of that :)

> Would be better create your menus to each sheet specifically? Use the
> Worksheet_SelectionChange event to switch between menus

I know that this is a possible approach, but it would mean using redundant code in
several different worksheet modules.  The workbook's SheetActivate event
allows me to have the code in one place, using the sh argument to help me
determine whether I am on one of the "special" worksheets where I need to
toggle the context menus.

Regards,

Patrick
 
11.01.2007 at 12:48PM PDT, ID: 20196060

Rank: Genius

Hello Patrick,

here another shot. I tried to avoud using the creating of command buttons. If you have some items to add to a menu then it might getting quite repeatable.

place this code in thisWorkbook code pane:
Private Sub Workbook_Open()
Call CreateMenu
End Sub
Private Sub Workbook_Deactivate()
ResetMenuBars
End Sub

Private Sub Workbook_Activate()
Call CreateMenu
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Call CreateMenu
End Sub

Place these codes in a new module
Dim CustomMenuSheets, tempMenu, cb1 As Object
Dim MenuType, CustSheets As Variant
Dim MacroSht1, MacroSht2 As Variant
Dim CaptionSht1, CaptionSht2 As Variant
Dim FaceSht1, FaceSht2 As Variant
Dim SheetCol As Collection
Dim MyDymanicArray(), MyMacroArray(), MyFaceArray() As Variant
Dim SheetCapArr, SheetMacroArr, SheetFaceArr As Variant
Dim a, i, j As Long
 Sub CreateMenu()

Call ResetMenuBars
'General Arrays
MenuType = Array("Cell", "Row", "Column")
CustSheets = Array("Sheet1", "Sheet2")
SheetCapArr = Array(CaptionSht1, CaptionSht2)
SheetMacroArr = Array(MacroSht1, MacroSht2)
SheetFaceArr = Array(FaceSht1, FaceSht2)

'Macro captions per sheet
CaptionSht1 = Array("Name1", "Name2", "Name3")
CaptionSht2 = Array("Name1", "Name4", "Name5")

'Macro names per sheet
MacroSht1 = Array("Macro1", "Macro2", "Macro3")
MacroSht2 = Array("Macro1", "Macro4", "Macro5")

'FaceIDs per sheet
FaceSht1 = Array(121, 122, 123)
FaceSht2 = Array(133, 134, 135)


If ValueInArray(ActiveSheet.Name, CustSheets) = False Then Exit Sub
    For a = 0 To UBound(CustSheets)
        If ActiveSheet.Name = CustSheets(a) Then
            For i = 0 To UBound(MenuType)
                Call ClearMenuBar(MenuType(i))
                Set tempMenu = Application.CommandBars(MenuType(i))
                    For j = 0 To Application.CountA(MacroSht1) - 1
                        Set cb1 = tempMenu.Controls.Add(msoControlButton)
               
                        With cb1
                            .Caption = SheetCapArr(a)(j)
                            .OnAction = SheetMacroArr(a)(j)
                            .FaceId = SheetFaceArr(a)(j)
                        End With
                    Next j
                j = 0
                Next i
        End If
    Next a
End Sub

Sub ClearMenuBar(val1)
'clears the specific menu bar
    Dim oCtrl As Object
   
    With Application.CommandBars(val1)
        'Clear the existing menus
        For Each oCtrl In .Controls
            oCtrl.Delete
        Next
    End With
End Sub

Sub ResetMenuBars()
'Reset the menubars
Application.CommandBars("Cell").Reset
Application.CommandBars("Row").Reset
Application.CommandBars("Column").Reset
End Sub

Function ValueInArray(val1, arr) 'val1 = value, arr = array
'Check if value exist in array
    TestArray = False
    On Error Resume Next
    N = Application.WorksheetFunction.Match(val1, arr, 0)
    If Err.Number = 0 Then
        ValueInArray = True
        Exit Function
    Else
        ValueInArray = False
        Exit Function
    End If
End Function

Perhaps this gives you some ideas.

regards,
Jeroen

 
11.02.2007 at 04:00PM PDT, ID: 20205042

Rank: Genius

hello patrick,

here an example file:
http://www.ee-stuff.com/Expert/Upload/getFile.php?fid=5358

though something strange is happening with this,
when opening the file while sheet1 is active then it results in an type match error. This is one of the sheets which is in the range to create a custom menu. if sheet3 is active then no error is shown. Though when sheet1 or 2 is selected, the macro to create the custom menu is working fine.



regards,

Jeroen
Assisted Solution
 
11.02.2007 at 04:39PM PDT, ID: 20205205
Jeroen,

Thanks!  I shall have a look...

Regards,

Patrick
 
11.03.2007 at 07:43AM PDT, ID: 20206984

Rank: Genius

No problems at all, Patrick.

Question Reopened.
 
11.03.2007 at 07:54AM PDT, ID: 20207033

Rank: Genius

No need to reopen this Q. I think the moderators have a lot of work to closing the questions which are listed in the Cleanup sections.

Thanks for your offer.
Jeroen
 
11.03.2007 at 07:57AM PDT, ID: 20207056

Rank: Genius

Already reopened, Jeroen, with my super-duper badger powers.
 
 
11.03.2007 at 07:30AM PDT, ID: 20206938
Sorry to see my solution didn't work out as you needed. I still think the strenght lies in that every sheet has its own customized menu and creating the buttons code is replaced in a way that you don't have to copy them over and over, and can be manged by using arrays to define the menu. So when additions have to be made it can be controled in one place, the arrays.

It also set the same menu to the cell, row and column popup menu. And every cell,row, column menu can be different on you specific sheets.

Jeroen
 
 
11.03.2007 at 07:31AM PDT, ID: 20206946
Wayne,

That was awesome!  I made some modifications, here is what I have so far:









Option Explicit
Option Compare Text

Dim CustomMenuSheets As Object
Dim CellMenu As CommandBar
Dim RowMenu As CommandBar
Dim ColumnMenu As CommandBar
Dim MenuColl As Collection

Private Sub Workbook_Open()

    Dim Counter As Long
    Dim DayName As String
   
    Set CustomMenuSheets = CreateObject("Scripting.Dictionary")
    With CustomMenuSheets
        For Counter = 1 To 7
            DayName = Format(DateSerial(2007, 1, Counter), "dddd")
            .Add DayName, DayName
        Next
    End With
   
    Set MenuColl = New Collection
    Set CellMenu = Application.CommandBars("Cell")
    MenuColl.Add CellMenu
    Set RowMenu = Application.CommandBars("Row")
    MenuColl.Add RowMenu
    Set ColumnMenu = Application.CommandBars("Column")
    MenuColl.Add ColumnMenu

End Sub

Private Sub Workbook_Activate()
   
    If CustomMenuSheets.Exists(ActiveSheet.Name) Then
        NewCellMenu
    Else
        ResetMenus
    End If

End Sub

Private Sub Workbook_Deactivate()
   
    ResetMenus

End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
   
    If CustomMenuSheets.Exists(Sh.Name) Then
        NewCellMenu
    Else
        ResetMenus
    End If
End Sub

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

    If CustomMenuSheets.Exists(Sh.Name) Then
        If Intersect(Target, Sh.[b23:bc36]) Is Nothing Then Cancel = True
    End If

End Sub

Private Sub NewCellMenu()
   
    Dim i As Long
    Dim x As CommandBar
    Dim cb As CommandBarControl
       
    For Each x In MenuColl
        'clear the controls from the menu
        For Each cb In x.Controls
            cb.Delete
        Next

        'add your new controls
        Dim cb1 As CommandBarButton
        Set cb1 = x.Controls.Add(msoControlButton)
        With cb1
            .Caption = "Control1"
            .OnAction = "Macro1"
            .Style = msoButtonCaption
        End With
   
        Dim cb2 As CommandBarButton
        Set cb2 = x.Controls.Add(msoControlButton)
        With cb2
            .Caption = "Control2"
            .OnAction = "Macro2"
            .Style = msoButtonCaption
        End With
   
        Dim cb3 As CommandBarButton
        Set cb3 = x.Controls.Add(msoControlButton)
        With cb3
            .Caption = "Control3"
            .OnAction = "Macro3"
            .Style = msoButtonCaption
        End With
   
        Dim cb4 As CommandBarButton
        Set cb4 = x.Controls.Add(msoControlButton)
        With cb4
            .Caption = "Control4"
            .OnAction = "Macro4"
            .Style = msoButtonCaption
        End With
   
        Dim cb5 As CommandBarButton
        Set cb5 = x.Controls.Add(msoControlButton)
        With cb5
            .Caption = "Control5"
            .OnAction = "Macro5"
            .Style = msoButtonCaption
        End With
    Next
       
    ' disable shortcut keys
    Application.OnKey "^c", "NoShortcut"
    Application.OnKey "^v", "NoShortcut"
    Application.OnKey "^x", "NoShortcut"
    Application.OnKey "^z", "NoShortcut"
    Application.OnKey "^y", "NoShortcut"
   
    ' disable edit menu
    Application.CommandBars("Worksheet Menu Bar").Controls("&Edit").Enabled = False
   
End Sub

Private Sub ResetMenus()

    CellMenu.Reset
    RowMenu.Reset
    ColumnMenu.Reset

    Application.OnKey "^c"
    Application.OnKey "^v"
    Application.OnKey "^x"
    Application.OnKey "^y"
    Application.OnKey "^z"

    Application.CommandBars("Worksheet Menu Bar").Controls("&Edit").Enabled = True

End Sub






That NoShortcut sub just shows a MsgBox telling the user those items are disabled.  Once I figure
out what goes on my customized context menu, I'll change that part of the code :)

Regards,

Patrick
 
 
11.03.2007 at 07:34AM PDT, ID: 20206952
Jeroen,

Perhaps I overlooked your suggestion unfairly.  As it happens, my immediate need was to have
a single menu for all of those worksheets, but in the future things may change.  I will ask to have
this Q reopened so that I can do a split, if Wayne has no objection...

Regards,

Patrick
 
 
 
20080236-EE-VQP-29 / EE_QW_EXPERT_20070906