
ลองปรับ Code เป็นตามด้านล่าง
ให้กด Shift ร่วมด้วยเพื่อเห็นว่าจุดแดงเลื่อนไปตำแหน่งใด ดู Status bar เพื่อเห็นว่าค่า x,y เป็นเท่าใด เมื่อได้ค่าที่ต้องการแล้วค่อยคลิก ค่าในกราฟคือค่าตามตำแหน่งจุดแดง การที่ตำแหน่งจุดแดงไม่ตรงกับเมาส์เพราะมีการแปลงตำแหน่ง Point ให้เป็น Pixel บนหน้าจอ
Code: Select all
Dim dXVal As Double
Dim dYVal As Double
Dim dXVal1 As Double
Dim dYVal1 As Double
Dim dZoom As Double
Dim dPixelSize As Double
Dim mchtChart As Chart
Private Sub Chart_MouseMove(ByVal Button As Long, _
ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
Set mchtChart = Charts(1)
On Error Resume Next
'The active window zoom factor
dZoom = ActiveWindow.Zoom / 100
'The pixel size, in points
dPixelSize = PointsPerPixel
'Mouse coordinates to (XY) Data coordinates
With mchtChart
dXVal = .Axes(xlCategory).MinimumScale + _
(.Axes(xlCategory).MaximumScale - _
.Axes(xlCategory).MinimumScale) * _
(x * dPixelSize / dZoom - _
(.PlotArea.InsideLeft + .ChartArea.Left)) / _
.PlotArea.InsideWidth
dYVal = .Axes(xlValue).MinimumScale + _
(.Axes(xlValue).MaximumScale - _
.Axes(xlValue).MinimumScale) * _
(1 - (y * dPixelSize / dZoom - _
(.PlotArea.InsideTop + .ChartArea.Top)) / _
.PlotArea.InsideHeight)
End With
Application.StatusBar = "(" & Round(dXVal, 2) & "," & Round(dYVal, 2) & ")"
'Mouse coordinates to Drawing Object Points
If Shift = 1 Then
With mchtChart
dXVal1 = (x * dPixelSize / dZoom - .ChartArea.Left)
dYVal1 = (y * dPixelSize / dZoom - .ChartArea.Top)
With .Shapes(1)
.Left = dXVal1 - .Width / 2
.Top = dYVal1 - .Height / 2
End With
End With
End If
End Sub
Private Sub Chart_MouseDown(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
With Worksheets("Sheet1")
If .Range("d1") = "" Then
.Range("d1") = Round(dXVal, 2)
.Range("e1") = Round(dYVal, 2)
Else
.Range("d65536").End(xlUp).Offset(1, 0) = Round(dXVal, 2)
.Range("e65536").End(xlUp).Offset(1, 0) = Round(dYVal, 2)
End If
End With
End Sub
ดูไฟล์แนบประกอบครับ

You do not have the required permissions to view the files attached to this post.