I want to be able to create a graph, with the top left of it being on my cursor position. Is that possible? Can the (X,Y) of my mouse be converted into a range format?
Once you're in Mouse settings, select Additional mouse options from the links on the right side of the page. In Mouse Properties, on the Pointer Options tab, at the bottom, select Show location of pointer when I press the CTRL key, and then select OK. To see it in action, press CTRL.
Microsoft Excel does not have the built-in functionality to get or set the cursor position. However, you can use the Declare statement in a Microsoft Excel Visual Basic for Applications macro to call a Microsoft Windows function to access the current position.
Select Options in the menu to open Excel Options. Select Advanced in the left pane of the dialog box. In the Editing Options section, go to After pressing Enter, move selection in the right pane. Select the down arrow next to Direction and choose up, left, or right.
The top left corner of the screen is (0, 0) i,e, X and Y coordinate is (0, 0). This means that vertical zero is topmost point and horizontal zero is the leftmost point. So, the task is to find the coordinates of the mouse over the screen. It can be find using the clientX and clientY property:
To View Cursor Coordinates Using the Coordinate Tracker. By: Help. Use the Coordinate Tracker to view the drawing X and Y coordinates of the cursor and a Z coordinate value read from a specified surface. Click Find. In the Coordinate Tracker, in the Surface list, select the surface from which Z values (elevations) are read.
Click Home tab Utilities panel ID Point. Find. Click the location that you want to identify. The X,Y,Z coordinate values are displayed at the Command prompt. With object snaps turned on, you can select an object and see the coordinates for a feature such as an endpoint, midpoint, or center.
The X,Y,Z coordinate values are displayed at the Command prompt. With object snaps turned on, you can select an object and see the coordinates for a feature such as an endpoint, midpoint, or center. Post a question.
Hm, it's not exactly built in AFAIK, but I found this page which gives a suggestion that worked for me:
In a module, put this at the top:
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, _
lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Then, for the subroutines to get the mouseX and mouseY, put this somewhere below:
Function MouseX(Optional ByVal hWnd As Long) As Long
' Get mouse X coordinates in pixels
'
' If a window handle is passed, the result is relative to the client area
' of that window, otherwise the result is relative to the screen
Dim lpPoint As POINTAPI
Application.Volatile(false)
GetCursorPos lpPoint
If hWnd Then ScreenToClient hWnd, lpPoint
MouseX = lpPoint.X
End Function
and
Function MouseY(Optional ByVal hWnd As Long) As Long
' Get mouse Y coordinates in pixels
'
' If a window handle is passed, the result is relative to the client area
' of that window, otherwise the result is relative to the screen
Dim lpPoint As POINTAPI
Application.Volatile(false)
GetCursorPos lpPoint
If hWnd Then ScreenToClient hWnd, lpPoint
MouseY = lpPoint.Y
End Function
Then, in Excel, if you simply enter into a cell =mouseX()
it'll return the mouseX position when you hit ENTER
. Same with =mouseY()
.
Trying it out, I did:
Sub chart_Test()
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveSheet.Shapes("Chart 1").Top = MouseY()
ActiveSheet.Shapes("Chart 1").Left = MouseX()
End Sub
and got it to work.
edit: Note, I'm not as good with charts as other things in VBA, so as you create charts, you'll need to edit the .Shapes("Chart 1").
part to whatever chart name/number you're on. Or iterate through them.
Not sure about the mouse x y but you could get the range on worksheet selection change. Put the chart at that location.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Chart.Left = Target.column
Chant.Top = Target.row
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With