简体   繁体   English

Excel双击功能区按钮以启动宏

[英]Excel double click on a ribbon button to initiate macro

I have a seemingly simple problem in Excel 2013 to which I cannot find an answer. 我在Excel 2013中有一个看似简单的问题,但找不到答案。 Below I described all the details. 下面我描述了所有细节。 Big thanks in advance for any help. 在此先感谢您的帮助。

Circumstances surrounding the problem: 围绕该问题的情况:

  1. I made a "QuickMacro.xlsm" workbook in Excel that contains Macro1. 我在包含Macro1的Excel中制作了一个“ QuickMacro.xlsm”工作簿。
  2. Secondly I added custom ribbon (through excel tools, not in VSTO) and on that ribbon I added said macro. 其次,我添加了自定义功能区(通过excel工具,而不是VSTO),并在该功能区上添加了“宏”。
  3. Now every time I open an excel workbook I have a quick accces to that macro across all workbooks on my computer (and the workbook with said macro is hidden, so I do not see it and it does not interfere with my work) 现在,每次我打开一个excel工作簿时,我都会在计算机上的所有工作簿中快速访问该宏(并且带有该宏的工作簿是隐藏的,因此我看不到它并且不会干扰我的工作)

My problem: I want Macro1 to initiate only when I double click its icon on the ribbon. 我的问题:我只希望双击功能区上的Macro1才能启动Macro1。 Single click ought to do nothing. 单击一下应该什么都不做。

My research on the problem: To find an answer I have went through many msdn materials on VSTO add ins and macros tutorials. 我对问题的研究:为了找到答案,我在VSTO add ins和macros教程上浏览了许多msdn资料。 The problem with former is that I am only a beginner in VSTO, and with latter that I find only "Worksheet_BeforeDoubleClick" events that regard cell activation, not ribbon button activation. 前者的问题是我只是VSTO的初学者,而后者的问题是我仅找到有关单元格激活而不是功能区按钮激活的“ Worksheet_BeforeDoubleClick”事件。

Question: Finally, my question is: how can it be done? 问题:最后,我的问题是:该怎么做? Is there a way to do it with Excel only or do I need to start learning advanced VSTO? 有没有办法仅使用Excel做到这一点,还是需要开始学习高级VSTO? Also, I would also appreciate any general tips on where to start digging to solve this problem (except msdn sites which I have analyzed extensively). 此外,我也将感谢您从何处开始挖掘以解决此问题的任何一般性提示(我已广泛分析的msdn站点除外)。 Any books maybe? 也许有书吗?

Tested by assigning this to a shape on a worksheet (so your Sub declaration may look different). 通过将其分配给工作表上的形状进行测试(因此您的Sub声明可能看起来有所不同)。

Sub MenuAction()

    Static t As Double

    '0.25 sec double-click interval
    If [NOW()] - t > (1 / 24 / 60 / 60 / 4) Then
        t = [NOW()]
        Exit Sub
    End If

    Debug.Print "double-click"
    'run your code here

End Sub

EDIT: a side note on use of [NOW()] vs. Now() . 编辑:使用[NOW()]Now()的附带说明。 I saw a post saying that these two expressions have different precision and that [NOW()] is more precise. 我看到一则帖子说这两个表达式具有不同的精度,并且[NOW()]更精确。

For example (also included Time for comparison): 例如(还包括比较Time ):

Sub Tester() Dim i, x 子测试器()Dim i,x

For i = 1 To 10
    'make a delay...
    For x = 1 To 3000#
        DoEvents
    Next x
    Debug.Print Time * 1, Now() * 1, [NOW()] * 1
Next i

End Sub 结束子

Output: 输出:

 Time                        Now()                       [NOW()]
 0.494270833333333           42891.4942708333            42891.4942753472 
 0.494270833333333           42891.4942708333            42891.4942768519 
 0.494270833333333           42891.4942708333            42891.4942782407 
 0.494270833333333           42891.4942708333            42891.4942795139 
 0.494270833333333           42891.4942708333            42891.4942810185 
 0.494282407407407           42891.4942824074            42891.4942824074 
 0.494282407407407           42891.4942824074            42891.494283912 
 0.494282407407407           42891.4942824074            42891.4942851852 
 0.494282407407407           42891.4942824074            42891.4942865741 
 0.494282407407407           42891.4942824074            42891.4942880787 

Interesting! 有趣!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM