热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

vba按钮-找到被点击的按钮-vbabutton-findwhichwasclicked

Ihaveassignedmacrotofewbuttons.我已经将宏分配给几个按钮。HowcanIfindoutinsidemacrowhichbuttonw

I have assigned macro to few buttons.

我已经将宏分配给几个按钮。

How can I find out inside macro which button was clicked?

如何在内部宏中查找单击了哪个按钮?

I am doing as user form, where he can input peoples from family:

我以用户形式做,他可以从家庭输入人:

name1:
surname1:

name1:surname1:

name2:
surname2:
|add next member|

name2:surname2:|添加下一个成员|

I wish button to appear always in last row of the last added person. For simplicity I think it is better to have like 100 empty forms in the sheet but all invisible at the begining.
Then when user clicks add next member I simply make next rows visible, and move button to next person. But to do that I need to know my current position.

我希望按钮始终显示在最后添加的人的最后一行中。为简单起见,我认为最好在表格中有100个空表格,但在开头时都是不可见的。然后当用户点击添加下一个成员时,我只需将下一行显示为可见,然后将按钮移动到下一个人。但要做到这一点,我需要知道我目前的立场。

Similar with deletion I would make rows invisible when remove button is clicked.

与删除类似,当单击删除按钮时,我会使行不可见。

name1:
surname1:
[remove]

name1:surname1:[删除]

name2:
surname2:
[remove]

name2:surname2:[删除]

name3:
surname3:
|add next member|

name3:surname3:|添加下一个成员|

I need to know which remove button was clicked.

我需要知道点击了哪个删除按钮。

EDIT: Found in web - what do you think, seems to be best /way

编辑:发现在网络 - 你怎么想,似乎是最好的/方式

Dim r As Range
Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column)).Select

4 个解决方案

#1


13  

I always write wrappers for each button that then call the macro in question.

我总是为每个按钮编写包装器,然后调用有问题的宏。

Like so:

像这样:

Public Sub StoreButton_Click()

  Call StoreTransValues(ActiveSheet)

End Sub

If you have only one button for any one page, you can just get the ActiveSheet property, and it will be the button on that page.

如果任何一个页面只有一个按钮,则只需获取ActiveSheet属性,它就是该页面上的按钮。


Edit:

编辑:

Here's the code to get and use the name of the calling button:

这是获取和使用调用按钮名称的代码:

Dim ButtonText As String

ButtOnText= Application.Caller

ActiveSheet.Shapes(ButtonText).Delete

You would use the .Move method to move the button.

您可以使用.Move方法移动按钮。

#2


6  

I finally found the solution for determining which button in a Worksheet was pushed. Credit is due to Derk at http://www.ozgrid.com/forum/showthread.php?t=33351.

我终于找到了确定工作表中哪个按钮被推送的解决方案。积分归功于Derk,网址为http://www.ozgrid.com/forum/showthread.php?t=33351。

My final example code:

我的最终示例代码:

Sub HereIAm()
    Dim b As Object
    Dim cs, rs As Integer
    Dim ss, ssv As String
    Set b = ActiveSheet.Buttons(Application.Caller)
    With b.TopLeftCell
        rs = .Row
        cs = .Column
    End With
    ss = Left(Cells(1, cs).Address(False, False), 1 - (ColNumber > 26)) & rs
    ssv = Range(ss).Value
    MsgBox "Row Number " & rs & "    Column Number " & cs & vbNewLine & _
        "Cell " & ss & "   Content " & ssv
End Sub

If you don't need the cells label, Cells(rs,cs).Value works as well.

如果您不需要单元格标签,则单元格(rs,cs).Value也可以。

#3


1  

Since you have a macro wired to your button(s), I assume you know which button it is that was clicked. To get the location of the button, use this:

由于您的按钮连接了一个宏,我假设您知道单击了哪个按钮。要获取按钮的位置,请使用以下命令:

ActiveSheet.Shapes("ButtonName").TopLeftCell.Address

To move a button to a new location, use this:

要将按钮移动到新位置,请使用以下命令:

Dim NewAddress as Range
NewAddress = ActiveSheet.Cells(5, 5) 'Or where ever you need it to go
ActiveSheet.Shapes("ButtonName").Left = NewAddress.Left
ActiveSheet.Shapes("ButtonName").Top = NewAddress.Top

#4


-2  

Dim button as a string:

    button = ActiveSheet.Shapes(Application.Caller).Name

推荐阅读
author-avatar
小攸罗曼史
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有