Disable Menu Item Based on ActiveCell Formula

edpolanco10

New Member
Joined
Oct 3, 2011
Messages
4
I have and excel addin for 2003 the adds a menu item to the right click menu. The addin works just find but I want to add the functionality where it is only enabled when the activecell contains a certain formula, lets call it “MyFunc()”. I know how to read the active formula from the active cell. I have a procedure that reads the formula. I also have a procedure that disables my menu item. But how can I get excel to call this procedure to disable my menu item when the activecell contains “MyFunc()”. I tried using the BeforeRightClick event in the thisworkbook but that only works on the active workbook.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
It sounds like you would want to use the SelectionChange event for the worksheet, not the BeforeRightClick. Simply check Target.Formula to see if it equals the formula you want to look for and, if it does, disable the menu item, otherwise enable it. Something like this I would imagine...

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Formula = "=MyFunc()" Then
    ' Code to enable the menu item
  Else
    ' Code to disable the menu item
  End If
End Sub
Remember, the If..Then test is case sensitive, so if you are testing against a UDF (user defined function) name, Excel won't automatically upper case the name like it does for its built-in functions, so you might want to so the test this way to be sure you account for variations in how a user types the function...

Code:
If UCase(Target.Formula) = "=MYFUNC()" Then
 
Upvote 0
Rick, Thanks for you reply. Putting the coded in SelectionChange event does work but only with the active workbook. When I put the code in the addin's SelectionChange event it doesn't work. Are the events procedures not available when the file is converted to an Addin?
 
Upvote 0
When I put the code in the addin's SelectionChange event it doesn't work.
Ah, AddIn... I missed that when I read your original message. I've not worked with them before, so I can only guess here. What if you put it in the AddIn's ThisWorkbook's SheetSelectionChange event... will that reach across to the ActiveWorkbook?
 
Upvote 0
Upvote 0
I got it! Thanks Rick.
With the help from that link I was able to figure it out. I essentially created a Class Module in my Add-In that encapsulates an Excel Application Object. Within the class module I used the SheetBeforeRightClick event to disable and enable my menu item depending on the formula contained in the ActiveCell. When the Add-In opens it instantiates Class Module by using the Workbook_Open() event. Below is simplified version of code
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
Add-In Code:
Dim MyMenuHandler As CMyClassModule<o:p></o:p>
Private Sub Workbook_Open()<o:p></o:p>
Set MyMenuHandler = New CMyClassModule<o:p></o:p>
End Sub
<o:p> </o:p>
Class Module:
Option Explicit<o:p></o:p>
Private WithEvents App As Application<o:p></o:p>
Private Sub App_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)<o:p></o:p>
If ActiveCell.Formula = "=MyFunc()" Then<o:p></o:p>
'enable menu code<o:p></o:p>
Else<o:p></o:p>
'disable menu code<o:p></o:p>
End If<o:p></o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
Private Sub Class_Initialize()<o:p></o:p>
Set App = Application<o:p></o:p>
End Sub
<o:p> </o:p>
 
Upvote 0
I got it! Thanks Rick.
With the help from that link I was able to figure it out.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Great! And you are quite welcome, of course. I thought the material at that link looked like it my give you a good head's up on how to do what you wanted.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,729
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top