2015-11-09 9 views
5

ボタンがどの行にあるかを識別するのに役立つこのコードブロックがあります。しかし、上記の行を非表示にすると、ボタンはその非表示行を参照します。例えばボタンが間違った行を参照しています

:ボタンは、行20にあり、私は私が行19と18、ボタンが戻る行18

の両方を非表示にした場合それは本当に奇妙だボタンが戻る行19をクリックすると、行19を非表示にした場合。ここで

は、私は、ボタンを作成するために使用されるブロックである:

Sub AddButtons() 
    Dim button As button 
    Application.ScreenUpdating = False 

    Dim st As Range 
    Dim sauce As Integer 

    For sauce = 10 To Range("F" & Rows.Count).End(xlUp).Row Step 1 
    Set st = ActiveSheet.Range(Cells(sauce, 11), Cells(sauce, 11)) 
    Set button = ActiveSheet.Buttons.Add(st.Left, st.Top, st.Width, st.Height) 

    With button 
     .OnAction = "GoToIssue.GoToIssue" 
     .Caption = "Go To Source" 
     .Name = "Button" & sauce 
    End With 
    Next sauce 
    Application.ScreenUpdating = True 
End Sub 

そして、ここでは、それがクリックされています一度ボタンの行のIDを返すブロックです:

Sub GoToIssue() 

    Dim b As Object 
    Dim myrow As Integer 

    Dim hunt As String 

    Set b = ActiveSheet.Buttons(Application.Caller) 
    With b.TopLeftCell 
     myrow = .Row 

    End With 


    hunt = Worksheets("Dummy").Range("F" & myrow).Value 

    'MsgBox hunt 

End Sub 

あなたの時間とヘルプ感謝しています。

+0

あなたは 'BottomRightCell'を試してみましたか?それは、ボタンのサイズとその拡大方法によって異なります。私はちょうどテストをしました。そして、 'BottomRightCell'は' Visible'セルを表示する点でより信頼性が高いようです。 –

答えて

2

あなたはこの機能を使用することができます。

Public Function FindButtonRow(btn As Object) As Long 
    Dim cell As Excel.Range 
    '------------------------------------------------- 

    Set cell = btn.TopLeftCell 

    Do While cell.EntireRow.Hidden 
     Set cell = cell.Offset(1, 0) 
    Loop 

    FindButtonRow = cell.row 

End Function 

TopLeftCellメソッドによって返された細胞が、隠された行にない場合は、チェックします。そうである場合、関数は、非表示の行からセルを検出している限り、セルを以下のように試行します。


あなたはそのようなあなたのサブルーチンでGoToIssueを使用することができます:

Sub GoToIssue() 

    Dim b As Object 
    Dim myrow As Integer 

    Dim hunt As String 

    Set b = ActiveSheet.Buttons(Application.Caller) 
    myrow = FindButtonRow(b) 

    hunt = Worksheets("Dummy").Range("F" & myrow).Value 

    'MsgBox hunt 

End Sub 
+0

魅力的な作品!ありがとうございました – Amostafa

関連する問題