如何创build一个macros来执行特定单元格的右键单击,并从右键单击菜单中select一个选项?

我认为这个问题很清楚。 我想在Excel 2007中设置一个macros来执行以下任务。
(1)右键单击一个特定的单元格,说A1。
(2)从右键菜单中select(左键单击)一个选项。

我真正需要的是将这个macros分配给一个命令button。

转到VBA代码编辑器(ALT + F11),并添加新的模块(插入 – >模块),然后粘贴下面的代码:

Option Explicit Public Const myBar As String = "MyPopupBar" Sub CreatePopup() Dim cmb As CommandBar Dim ctr As CommandBarControl DeletePopup Set cmb = Application.CommandBars.Add(myBar, msoBarPopup) Set ctr = cmb.Controls.Add(msoControlButton) With ctr .Caption = "Click me" .OnAction = "ClickMe" End With cmb.ShowPopup Set ctr = Nothing Set cmb = Nothing End Sub Sub ClickMe() MsgBox "You clicked me!", vbInformation, "Wow!" End Sub Sub DeletePopup() On Error Resume Next Application.CommandBars(myBar).Delete End Sub 

现在,双击Sheet1模块并添加以下代码:

 Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean) CreatePopup Cancel = True End Sub 

就这样!

[编辑]
你可以通过commandbutton调用popup式菜单,如下所示:

 Application.CommandBars("TheNameOfCommandBarPopup").ShowPopup 

欲了解更多信息,请参阅:
在所有版本的Excel中创buildpopup式菜单
在所有版本的Microsoft Excel中自定义上下文菜单

只是一些build议,不完整的答案,但也许是一个很好的开始:

 ?application.commandbars("Cell").Controls(2).caption 'this is your 2nd action in right clic menu, can set it to a variable of type commandbarControl 

&复制

 commandbars("Cell").Controls(2).execute 'will perform the action of what it is suposed to do when you clic it. 

所以也许用一个简单的testing(假设你知道该选项会做什么):

 with application.commandbars("Cell").Controls(2) if instr(1,.caption,"Copy")>0 then .execute end with 

编辑:如果选项2不是好的,你可以用长variablesreplace2并通过选项循环。 编辑2:我想,你知道如何select一个特定的单元格,这实际上是很容易的部分…