Interactive Chart Shows Start point and End Point Using MouseDown and MouseUp Event in Vba
Hi Friends !
Today I will discussed how to
select specified location inside a chart using Vba I am facing this problem
from 2-3 days but now I got exact solution to Operate such type of Problem. To explain
this scenario I took some data that by which I could create a chart and so I
generate a Sheet which contains some
data
Now, I select data
containing columns and press F11 and then it automatically generate a chart
sheet like this
Now I will go to developer tab and click on chart as circled
portion shown below inside image and
then a window will be opened
Inside that window paste the given code like this
Code
Dim startX As String
Dim startY As Double
Dim endX As String
Dim endY As Double
Private Sub
Chart_MouseDown(ByVal Button As Long, ByVal Shift As Long, _
ByVal x As Long, ByVal y As Long)
Dim ElementID As Long, Arg1 As Long, Arg2
As Long
Dim myX As Variant, myY As Double
With ActiveChart
' Pass x & y, return ElementID and
Args
.GetChartElement x, y, ElementID, Arg1,
Arg2
' Did we click over a point or data
label?
If ElementID = xlSeries Or ElementID =
xlDataLabel Then
If Arg2 > 0 Then
' Extract x value from array of
x values
myX = WorksheetFunction.Index _
(.SeriesCollection(Arg1).XValues, Arg2)
' Extract y value from array of
y values
myY = WorksheetFunction.Index _
(.SeriesCollection(Arg1).Values, Arg2)
startX = CDate(myX)
startY = myY
End If
End If
End With
End Sub
Private Sub
Chart_MouseUp(ByVal Button As Long, ByVal Shift As Long, _
ByVal x As Long, ByVal y As Long)
Dim ElementID As Long, Arg1 As Long, Arg2
As Long
Dim myX As Variant, myY As Double
With ActiveChart
' Pass x & y, return ElementID and
Args
.GetChartElement x, y, ElementID, Arg1,
Arg2
' Did we click over a point or data
label?
If ElementID = xlSeries Or ElementID =
xlDataLabel Then
If Arg2 > 0 Then
' Extract x value from array of
x values
myX = WorksheetFunction.Index _
(.SeriesCollection(Arg1).XValues,
Arg2)
' Extract y value from array of
y values
myY = WorksheetFunction.Index _
(.SeriesCollection(Arg1).Values, Arg2)
endX = CDate(myX)
endY = myY
' Display message box with
point information
MsgBox "X = "
& startX & vbCrLf _
& "Y = "
& startY & vbCrLf _
& "X = "
& endX & vbCrLf _
& "Y = "
& endY
End If
End If
End With
End Sub
Explanation
Inside this scenario I am using two events called MouseDown event and MouseUp event MouseDown event means left click on mouse and hold the mouse i.e Don't release it and MouseUp event shows to release mouse and after performing this Operation we will select start and end point of our selected area.
Comments
Post a Comment