vba 单击其中的表单按钮时获取单元格地址

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

Get the cell address when a form button in it is clicked

excelvba

提问by BoCode

I've multiple buttons in multiple cells and when any one of them is clicked, I've to perform a function.

我在多个单元格中有多个按钮,当单击其中任何一个按钮时,我必须执行一项功能。

I need to know which cell has this clicked button.

我需要知道哪个单元格有这个点击按钮。

采纳答案by Michael

While you're in Design Mode (meaning you can move and resize buttons), you can double click on a button and the VBA editor will open up with a subroutine for the Click event of that button. As mentioned above, they're not linked to the cell you happen to have them over in any way.

当您处于设计模式时(意味着您可以移动和调整按钮大小),您可以双击一个按钮,VBA 编辑器将打开该按钮的 Click 事件的子例程。如上所述,它们并没有以任何方式与您碰巧拥有它们的细胞相关联。

回答by Arran549

  Dim r As Range
  Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell

The above code will give you the cell address of the button (This only works for form buttons not active x buttons).

上面的代码将为您提供按钮的单元格地址(这仅适用于表单按钮而不是活动 x 按钮)。

回答by julien

For some reasons I was obliged to use .Shapes(Application.Caller) because the .Buttons(Application.Caller) was returning a bug.

由于某些原因,我不得不使用 .Shapes(Application.Caller) 因为 .Buttons(Application.Caller) 返回了一个错误。

Dim r As Range
  Set r = ActiveSheet.Shapes(Application.Caller).TopLeftCell

回答by Kinia

My suggestion is to try this:

我的建议是试试这个:

Dim r As Range
Set r = ActiveSheet.Shapes(Application.Caller).TopLeftCell
MsgBox r.Address

Here you can get the address where the button is placed

在这里可以得到按钮所在的地址

回答by Lunatik

Form controls aren't linked directly to worksheet cells. They can be 'anchored' to them for size & alignment purposes, but don't as far as I know have any relationship with the cell that would allow you to get a cell reference, assuming one could even be determined without ambiguity.

表单控件不直接链接到工作表单元格。为了大小和对齐目的,它们可以“锚定”到它们上,但据我所知,不要与允许您获得单元格参考的单元格有任何关系,假设甚至可以毫无歧义地确定一个单元格。

Each form control can only run one macro though, so surely it is easy to identify that way, no?

虽然每个表单控件只能运行一个宏,所以肯定很容易识别这种方式,不是吗?