vba 双击事件 - 多个范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21558377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:44:14  来源:igfitidea点击:

Double Click Event - Multiple Ranges

excelvbadouble-clickcode-structure

提问by Josh

I'm looking for the best way to code in multiple ranges for my double click event.

我正在寻找为我的双击事件在多个范围内编码的最佳方法。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A3:A25")) Is Nothing Then
    'code
End If
End Sub

As you see above, when A3 to A25 is clicked, the double click event takes place. But I also have other sections throughout the sheet that I want to include to set off the event. A29:A40, F3:F37, K3:K40, P3:P40.

如上所示,当单击 A3 到 A25 时,会发生双击事件。但我还想在整个工作表中包含其他部分以引发事件。A29:A40、F3:F37、K3:K40、P3:P40。

What is the best way to code that without adding new 'If' blocks?
Or is adding the new 'If' blocks (and calling a subroutine) the best way?

在不添加新的“If”块的情况下进行编码的最佳方法是什么?
还是添加新的“If”块(并调用子程序)是最好的方法?

回答by Dmitry Pavliv

Use this one:

使用这个:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("A3:A25, A29:A40, F3:F37, K3:K40, P3:P40")) Is Nothing Then
        'code
    End If
End Sub