PDA

View Full Version : VBA SetCursosPos with co-ordinates of cell value



rob88
05-09-2013, 01:31 AM
Hi, i have a table containing the co-ordinates of different areas of a program that i want to be able to intereactive with from excell.


I am using SetCursorPos with click events to move the mouse to different areas and click - which all works fine in the sense that if i put in the line of code (for example)

Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10


SetCursorPos 200, 100 'x and y position
mouse_event &H2, 0, 0, 0, 0
mouse_event &H4, 0, 0, 0, 0


then the mouse is moved to 200, 100 and a click is initiated (which is fine)

my problem is i want to be able to to replace the 200, 100 with a cell value (in which i will have co ordinates)

for example

SetCursorPos range("A1").Value 'x and y position
mouse_event &H2, 0, 0, 0, 0
mouse_event &H4, 0, 0, 0, 0

where A1 = 200, 100


but if i try to run this i get 'compile error : argument not optional'

Anyone have any ideas? This must be possible as range("A1").Value = 200, 100 so
SetCursorPos range("A1").Value 'x and y position
is the same as
SetCursorPos 200,100 'x and y position



Thanks for any help :)

snb
05-09-2013, 02:43 AM
SetCursorPos cells(1,1).value, cells(1,2).value

SetCursorPos has 2 required arguments, separated by a comma.

Paul_Hossler
05-09-2013, 07:30 AM
If A1 truly has the string text in the post then you'd probably have to break out the X and Y before you could use them


'Assuming that A1 contains a string: A1.Value = "200, 100"
Sub s()
Dim v As Variant

v = Split(Range("a1").Value, ",")

SetCursorPos CDbl(v(0)), CDbl(v(1)) 'x and y position
End Sub


Paul

rob88
05-09-2013, 08:00 AM
Thank you both for the help. I have it working now :clap: :bow:

TheSprice17
07-09-2014, 06:00 AM
I am wondering if I can do something similar to this, except instead of using x and y coordinates, set the cursor position to a specific cell. For example:

SetCursorPos Cells(1,1).Left, Cells(1,1).Top
But obviously that doesnt work

GTO
07-10-2014, 11:15 PM
Off the top, I believe you'll need to convert .left and .top from Points to Pixels accurately, and figure out where the workbook's window (EXCEL7 maybe?) is located in the scheme of things. If you are looking to pursue this, I would suggest starting your own thread.

Mark

snb
07-11-2014, 02:37 AM
To keep it schort:


sub M_snb()
cells(1).select
End sub