2016-08-30 10 views
2

行をクリックしたボタンを取得するにはどうすればよいですか?

こんにちは私はこっちにいくつかの助けを必要とするすべての人;)Excel VBA - Buttonインターフェイスオブジェクトの対応する範囲を取得

私はExcelでボタンを作るしようとしていますVBAで初心者が、私がクリックしたボタンの行を把握する方法について困惑しています。

:写真のように私は、私はボタン「Sélectionnerの国連ポステ」をクリックすると、その後、彼はここで彼の位置(iはどの行でクリックされたボタン)

は、ボタンを作成するための私のコードである私に教えてくれることを望みます私は名前がAssocierSessoinCandidatureあるアプリケーションウィンドウを持って

Sub PosteBtAction() 
    AssocierSessoinCandidature.Show 
End Sub 

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 

はここでイベントボタンのための私のコードです。実際にクリックした位置を取得して、この情報をアプリケーションウィンドウに送信したいと考えています。ここで

は私の例Excelのシートである:

答えて

3

通話ボタンが範囲を得るために

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 
0

私は非常によく似たものを使用している、あなたは以下のコードに適応することができます:ボタンをクリックすると

Sub Button24_Click() 
Dim strShape As String 
strShape = Shapes("button24").TopLeftCell.Address 
CallAnotherFunction strShape 
End Sub 

を、このコードの範囲がかかりますボタン24を文字列として使用しています(範囲を使用しなかった理由はわかりません)。CallAnotherFunction

要するに、これは必要なものです:

Shapes("button24").TopLeftCell.Address 

または

Shapes("button24").TopLeftCell.Row 
0

あなたはTopLeftCellためButtonオブジェクトのプロパティにアクセスすることができますし、BottomRightCellをクリックすると、以下のサブは、バインドされたことに対処しますコントロール:

Option Explicit 

Sub Test() 

    Dim ws As Worksheet 
    Dim btn As Object 
    Dim strAddressTopLeft As String 
    Dim strAddressBottomRight As String 
    Dim strButtonName As String 

    Set ws = ActiveSheet 

    For Each btn In ws.Buttons 
     strAddressTopLeft = btn.TopLeftCell.Address 
     strAddressBottomRight = btn.BottomRightCell.Address 
     strButtonName = btn.Name 
     Debug.Print "Range of button (" & strButtonName & "): " & _ 
      strAddressTopLeft & ":" & _ 
      strAddressBottomRight 
    Next btn 

End Sub 
関連する問題