エクセル VBA マウスカーソルの移動とクリック
セルの固定をしていると うまくいかない ので放棄した
Excel VBA マウスカーソルの位置とセルを連携する | やさしいExcelVBA
エクセルExcel大事典 VBAマクロ応用講座 スクリーン座標 ポイント ピクセル PointsToScreenPixels
VBAでマウスを自動操作する方法~自動打鍵を目指して~ – Rainbow Planet
#If VBA7 And Win64 Then
'マウスカーソルの位置を取得するAPI
Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
'マウスイベントを取得するための決まり文句的なもの
Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Declare PtrSafe Sub mouse_event Lib "user32" ( _
ByVal dwFlags As Long, _
Optional ByVal dx As Long = 0, _
Optional ByVal dy As Long = 0, _
Optional ByVal dwDate As Long = 0, _
Optional ByVal dwExtraInfo As Long = 0 _
)
#Else '32bit用
'マウスカーソルの位置を取得するAPI
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
'マウスイベントを取得するための決まり文句的なもの
Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Declare Sub mouse_event Lib "user32" ( _
ByVal dwFlags As Long, _
Optional ByVal dx As Long = 0, _
Optional ByVal dy As Long = 0, _
Optional ByVal dwDate As Long = 0, _
Optional ByVal dwExtraInfo As Long = 0 _
)
#End If
Sub Sample1()
Dim p As POINTAPI 'API用変数
GetCursorPos p 'カーソル位置取得
MsgBox "X座標:" & p.x & " Y座標:" & p.y
End Sub
Excel VBA マウスカーソルの位置とセルを連携する | やさしいExcelVBA
Option Explicit
エクセルExcel大事典 VBAマクロ応用講座 スクリーン座標 ポイント ピクセル PointsToScreenPixels
Sub フォームコントロールボタンをクリック()
Dim ws01 As Worksheet
Set ws01 = Worksheets("入力")
Dim btn As Button
Set btn = ws01.Buttons("情報取得ボタン")
' btn.Select
Debug.Print Selection.Width * 96 / 72 * (ActiveWindow.Zoom / 100)
ws01.Range("A1").Select
Dim R1C1Left, R1C1Top As Long
R1C1Left = ActiveWindow.PointsToScreenPixelsX(Selection.Left)
R1C1Top = ActiveWindow.PointsToScreenPixelsY(Selection.Top)
Range("a1") = R1C1Left
Range("a2") = R1C1Top
Dim x, y As Long
x = ((btn.Left * 96 / 72) * (ActiveWindow.Zoom / 100))
y = ((btn.Top * 96 / 72) * (ActiveWindow.Zoom / 100))
Debug.Print x
SetCursorPos y, x
Application.Wait Now + TimeValue("00:00:01")
'左クリックの動作
mouse_event 2
mouse_event 4
End Sub
Sub フォームコントロールのボタンを捕らえる()
Dim ws01 As Worksheet
Set ws01 = Worksheets("入力")
' ActiveSheet.Shapes("情報取得ボタン").Select
' Dim btn As Button
' Set btn = ActiveSheet.Buttons(2)
' Debug.Print btn.Caption
''' Dim x, y As Long
''' x = ActiveWindow.PointsToScreenPixelsX(ActiveSheet.Shapes("情報取得ボタン").Left * 96 / 72)
''' y = ActiveWindow.PointsToScreenPixelsY(ActiveSheet.Shapes("情報取得ボタン").Top * 96 / 72)
''' x = ActiveWindow.PointsToScreenPixelsX(ActiveSheet.Range("B3").Left * 96 / 72)
''' y = ActiveWindow.PointsToScreenPixelsY(ActiveSheet.Range("B3").Top * 96 / 72)
'''
''' Debug.Print x
''' SetCursorPos x, y
'左クリックの動作
'mouse_event 2
'mouse_event 4
'(実行用 選択中のセル又はオブジェクトの位置)
Dim R1C1Left As Long
Dim R1C1Top As Long
Const DPI As Long = 96
Const PPI As Long = 72
R1C1Left = ActiveWindow.PointsToScreenPixelsX(0)
Debug.Print R1C1Left
R1C1Top = ActiveWindow.PointsToScreenPixelsY(0)
Debug.Print R1C1Top
Range("a1") = ((Selection.Left * DPI / PPI) * (ActiveWindow.Zoom / 100)) + R1C1Left
Range("a2") = ((Selection.Top * DPI / PPI) * (ActiveWindow.Zoom / 100)) + R1C1Top
'(検証用 現在のマウスカーソルの位置)
Dim Pos As POINTAPI
Dim MouseLeft As Long
Dim MouseTop As Long
GetCursorPos Pos
MouseLeft = Pos.x
MouseTop = Pos.y
Range("a3") = MouseLeft
Range("a4") = MouseTop
Debug.Print Selection.Height * DPI / PPI * (ActiveWindow.Zoom / 100)
Debug.Print Selection.Width * DPI / PPI * (ActiveWindow.Zoom / 100)
End Sub
VBAでマウスを自動操作する方法~自動打鍵を目指して~ – Rainbow Planet




