Sub UpdateChartAboveClickedButton()
Dim wb As Workbook
Dim sht As Worksheet
Dim btn_clicked As Shape
Dim btn_location As Range
Dim cht_to_update As ChartObject
Dim sngChartBottomEdge As Single
Dim sngTopClickedBtn As Single
Set wb = ThisWorkbook 'myworkbook
Set sht = ActiveSheet 'wb.myworksheet
Set btn_clicked = sht.Shapes(Application.Caller) 'find clicked button
sngTopClickedBtn = btn_clicked.Top
For Each cht_to_update In sht.ChartObjects
sngChartBottomEdge = cht_to_update.Top + cht_to_update.Height
'Find chart object directly above the clicked button.
'The margin of inaccuracy of the button placement up to 20% of the button height.
If sngTopClickedBtn <= sngChartBottomEdge + btn_clicked.Height * 0.2 And _
sngTopClickedBtn >= sngChartBottomEdge - btn_clicked.Height * 0.2 Then
Exit For
End If
Next cht_to_update
If cht_to_update Is Nothing Then
MsgBox "No chart found that meets the assumptions!", vbExclamation
Exit Sub
End If
With cht_to_update
MsgBox .Name & vbLf & .TopLeftCell.Address & ":" & .BottomRightCell.Address
End With
End Sub
The top edge of the button does not have to be exactly on the bottom edge of the chart. In this code, you can have button location inaccuracy up to 20% of the button height.
Artik
Bookmarks