Hi Franz,
Could you please explain a bit more in detail? I tried to use that
event but I couldn't make it work. Do I have to assign a macro to the
chart? How would the macro look like to capture the mouse buttons and
position?
If this chart is in a chartobject (ie, on a worksheet), I believe you would
have to create an event sink for the chart using a class module. Here are
the steps to do that:
1) Insert a new class module and name it "CChartSink"
2) Add the following code to that class module:
Public WithEvents cht As Excel.Chart
Private Sub cht_MouseUp(ByVal Button As Long, ByVal Shift As Long, _
ByVal x As Long, ByVal y As Long)
MsgBox "Button: " & Button
MsgBox "Shift: " & Shift
MsgBox "X: " & x & ", Y: " & y
End Sub
3) Put the following code in a standard module:
Private mobjChartSink As CChartSink
Private Sub mStartChartSink(rcht As Excel.Chart)
Set mobjChartSink = New CChartSink
Set mobjChartSink.cht = rcht
End Sub
Private Sub mStopChartSink()
On Error Resume Next
Set mobjChartSink.cht = Nothing
Set mobjChartSink = Nothing
End Sub
Public Sub gDemoStart()
mStartChartSink Worksheets("Sheet1").ChartObjects(1).Chart
End Sub
Public Sub gDemoStop()
mStopChartSink
End Sub
4) Change the references in the gDemoStart routine as needed (worksheet
name, chartobject name or index).
5) Close the VBE, then run the macro gDemoStart.
6) When you're done testing, run gDemoStop.
When you're done testing, you could call the gDemoStart routine from the
Workbook_Open event and the gDemoStop routine from the Workbook_BeforeClose
event.
A few things I noticed:
1) The clicking is a little strange - the user may have to click twice on
the chart in order to trigger the event (if the first click is not on the
chart itself or one of the chart labels).
2) To remedy #1, I tried setting the locked property of the chartobject and
protecting the worksheet. However, this disables clicking on the chart and
thus the events don't fire.
Another alternative you could try is the Windows API function GetCursorPos.
Paste the following code into a standard code module:
Public Type POINTAPI
x As Long
y As Long
End Type
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As
Long
Sub DemoWithAPI()
Dim ptAPI As POINTAPI
Dim lChartX As Long
Dim lChartY As Long
GetCursorPos ptAPI
With Worksheets("Sheet1").ChartObjects(1)
lChartX = ptAPI.x - .Left
lChartY = ptAPI.y - .Top
End With
MsgBox "X:" & lChartX & ", Y:" & lChartY
End Sub
Then assign the macro DemoWithAPI to the chartobject. Now, when the user
clicks the chart, the actual *Screen* position in pixels will be displayed.
I'm not entirely sure how to convert those coordinates to the equivalent
chart coordinates, but maybe you can play with it a bit.
--
Regards,
Jake Marx
MS MVP - Excel
www.longhead.com
[please keep replies in the newsgroup - email address unmonitored]