vba 设置 BeforeDoubleClick 的范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24088437/
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 03:23:15  来源:igfitidea点击:

Set a range for BeforeDoubleClick

excelvbaexcel-vba

提问by Dennis R

I'm trying to add DoubleClick event for a range of cells starting from B:27 to B:x(any number of rows). So I want to restric to only column 'B' starting from row # 27

我正在尝试为从 B:27 到 B:x(任意行数)的一系列单元格添加 DoubleClick 事件。所以我想限制为仅从第 27 行开始的“B”列

When any cell in the defined range is clicked I'll update the Target.Value in another cell and call a sub routine.

单击定义范围内的任何单元格时,我将更新另一个单元格中的 Target.Value 并调用子例程。

The current code looks like this. This is from a previous developer and it works in one of the sheet but now I'm creating a similar report on another sheet and it gives me the error

当前代码如下所示。这是来自以前的开发人员,它在其中一个工作表中工作,但现在我在另一张工作表上创建了一个类似的报告,它给了我错误

Method 'Range' of object '_Worksheet' failed.

I'm not sure where "DblClikRange"range is defined, I looked in Module code as well I searched for the word throughout tha VBA code I could not find it.

我不确定"DblClikRange"范围在哪里定义,我也查看了模块代码,我在整个 VBA 代码中搜索了这个词,但我找不到它。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
   If Not Intersect(Target, Range("DblClikRange")) Is Nothing Then
     On Error Resume Next

     Range("B22") = Target.Value

    ' call the sub to load data
      Call MyDemo
   End If
End Sub

Any idea where to look up for this named range variable or how to do this in a different way? Any help would be appreciated.

知道在哪里查找此命名范围变量或如何以不同的方式执行此操作吗?任何帮助,将不胜感激。

采纳答案by Dave.Gugg

You don't need to used a named range if you don't want to, you can just explicitly define the cells in the code:

如果您不想,您不需要使用命名范围,您可以在代码中明确定义单元格:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("B:B")) Is Nothing Then
         If Target.Row => 27 Then
             Range("B22") = Target.Value
             ' call the sub to load data
             Call MyDemo
         End If
    End If
End Sub

It really just depends on what you are most comfortable with - it seems like you aren't used to working with named ranges, so just use the code above.

这实际上取决于您最喜欢什么 - 似乎您不习惯使用命名范围,因此只需使用上面的代码。