Private Declare Function GetPixel Lib "gdi32" (ByVal hDC&, ByVal x&, ByVal y&) As Long
Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINT) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd&) As Long
Private Type POINT
x As Long
y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd&, ByVal hDC&) As Long
Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex&) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC&, ByVal nIndex&) As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Function ScreenDPI&(bVert As Boolean)
Static lDPI&(1), lDC&
If lDPI(0) = 0 Then
lDC = GetDC(0)
lDPI(0) = GetDeviceCaps(lDC, 88&) 'horz
lDPI(1) = GetDeviceCaps(lDC, 90&) 'vert
lDC = ReleaseDC(0, lDC)
End If
ScreenDPI = lDPI(Abs(bVert))
End Function
Private Function PTtoPX&(Points!, bVert As Boolean)
PTtoPX = Points * ScreenDPI(bVert) / 72
End Function
Sub Picture1_Click() ' assign this to the picture
Dim pLoc As POINT, Colour&, lDC, wnd As Window, rng As Range, rc As RECT, i%
Set wnd = ActiveCell.Parent.Parent.Windows(1)
lDC = GetWindowDC(0)
GetCursorPos pLoc
Colour = GetPixel(lDC, pLoc.x, pLoc.y)
i = 0
Set rng = wnd.VisibleRange.Cells(1, 1)
Do
Set rng = rng.Offset(1)
i = i + 1
If i > 50 Then Exit Do
GetRangeRect rng, rc
Loop While i < wnd.VisibleRange.Rows.Count And rc.Top < pLoc.y
If Colour = 16777215 Then
MsgBox "You did not click a line."
Else
MsgBox "The worksheet row is " & rng.Row - 1
End If
End Sub
Sub GetRangeRect(ByVal rng As Range, ByRef rc As RECT)
Dim wnd As Window
Set wnd = rng.Parent.Parent.Windows(1)
With rng
rc.Left = PTtoPX(.Left * wnd.Zoom / 100, 0) + wnd.PointsToScreenPixelsX(0)
rc.Top = PTtoPX(.Top * wnd.Zoom / 100, 1) + wnd.PointsToScreenPixelsY(0)
rc.Right = PTtoPX(.Width * wnd.Zoom / 100, 0) + rc.Left
rc.Bottom = PTtoPX(.Height * wnd.Zoom / 100, 1) + rc.Top
End With
End Sub