Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
784 views
in Technique[技术] by (71.8m points)

vba - Get corresponding Range for Button interface object

I want when I click the button "Sélectionner un poste" it will tell me the position. (The button which I clicked in which row.)

Code to create the button:

Sub AjouterBoutonPoste(positionX As Integer, positionY As Integer, nom As String)
    Set t = ActiveSheet.Range(Cells(positionX, positionY), Cells(positionX, positionY))
    Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
    With btn
        .OnAction = "PosteBtAction"
        .Caption = "Sélectionner un poste"
        .Name = nom & CStr(positionX) & CStr(positionY)
    End With
End Sub

Code for the event button:

Sub PosteBtAction()
   AssocierSessoinCandidature.Show
End Sub

I have an application window named AssocierSessoinCandidature. I want the position which I clicked sent to the application window.

Here is my example Excel sheet:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Call the below Sub when the button is clicked

Sub foo()

Dim obj As Object
Dim row_no As Integer

Set obj = ActiveSheet.Buttons(Application.Caller)
With obj.TopLeftCell
    row_no = .Row
End With
MsgBox "The Button is in the row number " & row_no

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...