GetCursorPos地址在Shape对象下面

时间:2017-11-14 18:22:49

标签: excel vba

我有一个代码,用于显示光标位置(以像素为单位)和电子表格中的等效范围。只要将光标放在Shape对象上,代码就会产生错误,因为它无法检索Shape下面的单元格值。那有什么解决方法吗?以下是代码:

Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Type POINTAPI
Xcoord As Long
Ycoord As Long
End Type

Sub SetKey()

    Application.OnKey "a", "PositionXY"

End Sub


Sub PositionXY()
Dim llCoord As POINTAPI
Dim rng As Range

Do
GetCursorPos llCoord
Set rng = GetRange(llCoord.Xcoord, llCoord.Ycoord)

Range("A1").Value = "X: " & llCoord.Xcoord & " Y: " & llCoord.Ycoord
Range("A2").Value = rng.Address
DoEvents
Loop
End Sub

Function GetRange(x As Long, y As Long) As Range
    Set GetRange = ActiveWindow.RangeFromPoint(x, y)
End Function

1 个答案:

答案 0 :(得分:0)

通过在Shape对象上放置Label并为其分配以下代码来处理它:

a={"1" : 3, "2" : 3, "6":3}