ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Macro - onAction arguments (https://www.excelbanter.com/excel-worksheet-functions/261189-macro-onaction-arguments.html)

version83

Macro - onAction arguments
 
Hello.
I have this code:

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Application.CommandBars("Cell").Reset
For Each Worksheet In Application.Worksheets
With Application.CommandBars("Cell").Controls
With .Add
.Caption = Worksheet.Name
.OnAction = "someMacro"
.Tag = "someTag"
.BeginGroup = True
End With
End With
Next
End Sub

It adds all sheets in the context menu.
There is just one more thing to be done.
When you click on a sheet from the context menu, it must became active.
Something like .OnAction = Worksheet.Select or a separate macro ....
I am not sure how to do it so i need some advice.

Dave Peterson

Macro - onAction arguments
 
Excel keeps track of what control you click on and you can use that:

Option Explicit
Sub SomeMacro()
MsgBox Application.CommandBars.ActionControl.Caption
thisworkbook.worksheets(Application.CommandBars.Ac tionControl.Caption) _
.select
End Sub

But...
It's not a good idea to use a variable that shares a name with a VBA keyword
(like Worksheet).

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

Dim wks As Worksheet

'this is usually a bad idea!
'You just reset the user's rightclick menu popup!
Application.CommandBars("Cell").Reset

For Each wks In Me.Worksheets
With Application.CommandBars("Cell").Controls
With .Add
.Caption = wks.Name
.OnAction = "'" & Me.Name & "'!someMacro"
.Tag = "someTag"
.BeginGroup = True 'between each name????
End With
End With
Next wks
End Sub

And another but...

I wouldn't want you to reset my popup toolbar. I have a personal.xla (or .xls)
that I use to modify my toolbars -- including the Cell popup.

Instead, you may want to look at this alternative from Debra Dalgleish's site:
http://contextures.com/xlToolbar01.html
And she points to an xl2007 version by Ron de Bruin:
http://contextures.com/xlToolbar01b.html



version83 wrote:

Hello.
I have this code:

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal
Target As Range, Cancel As Boolean)
Application.CommandBars("Cell").Reset
For Each Worksheet In Application.Worksheets
With Application.CommandBars("Cell").Controls
With .Add
Caption = Worksheet.Name
OnAction = "someMacro"
Tag = "someTag"
BeginGroup = True
End With
End With
Next
End Sub

It adds all sheets in the context menu.
There is just one more thing to be done.
When you click on a sheet from the context menu, it must became
active.
Something like .OnAction = Worksheet.Select or a separate macro ....
I am not sure how to do it so i need some advice.

--
version83


--

Dave Peterson

Dave Peterson

Macro - onAction arguments
 
This macro has a bug in it.

Option Explicit
Sub SomeMacro()
MsgBox Application.CommandBars.ActionControl.Caption
thisworkbook.worksheets(Application.CommandBars.Ac tionControl.Caption) _
.select
End Sub

It'll only appear when the workbook with the code isn't the activeworkbook.

Try:

Option Explicit
Sub SomeMacro()
MsgBox Application.CommandBars.ActionControl.Caption
thisworkbook.activate '<-- added
thisworkbook.worksheets(Application.CommandBars.Ac tionControl.Caption) _
.select
End Sub

(but I still don't like messing up that Cell popup menu!)

Dave Peterson wrote:

Excel keeps track of what control you click on and you can use that:

Option Explicit
Sub SomeMacro()
MsgBox Application.CommandBars.ActionControl.Caption
thisworkbook.worksheets(Application.CommandBars.Ac tionControl.Caption) _
.select
End Sub

But...
It's not a good idea to use a variable that shares a name with a VBA keyword
(like Worksheet).

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

Dim wks As Worksheet

'this is usually a bad idea!
'You just reset the user's rightclick menu popup!
Application.CommandBars("Cell").Reset

For Each wks In Me.Worksheets
With Application.CommandBars("Cell").Controls
With .Add
.Caption = wks.Name
.OnAction = "'" & Me.Name & "'!someMacro"
.Tag = "someTag"
.BeginGroup = True 'between each name????
End With
End With
Next wks
End Sub

And another but...

I wouldn't want you to reset my popup toolbar. I have a personal.xla (or .xls)
that I use to modify my toolbars -- including the Cell popup.

Instead, you may want to look at this alternative from Debra Dalgleish's site:
http://contextures.com/xlToolbar01.html
And she points to an xl2007 version by Ron de Bruin:
http://contextures.com/xlToolbar01b.html

version83 wrote:

Hello.
I have this code:

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal
Target As Range, Cancel As Boolean)
Application.CommandBars("Cell").Reset
For Each Worksheet In Application.Worksheets
With Application.CommandBars("Cell").Controls
With .Add
Caption = Worksheet.Name
OnAction = "someMacro"
Tag = "someTag"
BeginGroup = True
End With
End With
Next
End Sub

It adds all sheets in the context menu.
There is just one more thing to be done.
When you click on a sheet from the context menu, it must became
active.
Something like .OnAction = Worksheet.Select or a separate macro ....
I am not sure how to do it so i need some advice.

--
version83


--

Dave Peterson


--

Dave Peterson


All times are GMT +1. The time now is 01:19 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com