Excel VBA: getting row of clicked button [duplicate]

邮差的信 提交于 2019-11-27 14:04:18

Each Shape has TopLeftCell property. It contains a cell within which the top left corner of the shape resides.

There's a great solution mentioned here: http://www.ozgrid.com/forum/showthread.php?t=33351&p=167317#post167317

Golden code copied from the above post:

Sub Mainscoresheet() 
     ' Mainlineup Macro
    Dim b As Object, cs As Integer 
    Set b = ActiveSheet.Buttons(Application.Caller) 
    With b.TopLeftCell 
        cs = .Column 
    End With 
    MsgBox "Column Number " & cs 
End Sub 
Robert Nagtegaal

Excellent answer. Btw It also works for Rownumber!

'Same for rownumbers!
Sub Mainscoresheet() 
     ' Mainlineup Macro
    Dim b As Object, RowNumber As Integer 
    Set b = ActiveSheet.Buttons(Application.Caller) 
    With b.TopLeftCell 
        RowNumber = .Row
    End With 
    MsgBox "Row Number " & RowNumber 
End Sub
user3099993

This works too! Selects the cell that the generated button resides in (knew it was in column "K" but that could be calculated too!.

ActiveSheet.Range("K" & ActiveSheet.Buttons(Application.Caller).TopLeftCell.Row).Select
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!