G_Seattle said:
Is there a way of knowing where the mouse pointer is, as is what cell is it currently over?
You know what, Jaafar and I have been going back and forth on similar issues for some time now, and he devised a really special method of conditional formatting based on mouse move over naked cells.
Below is his code, modified to show the "moused over" cell address in cell A1.
Note, 99.9999% of this code is Jaafar's braintrust; I only did a slight modification for the address display in A1.
In a standard module:
Option Explicit
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Type POINTAPI
x As Long
Y As Long
End Type
Dim lngCurPos As POINTAPI
Dim TimerOn As Boolean
Dim TimerId As Long
Public oldColor As Long
Dim newRange As Range
Dim oldRange As Range
Sub StartTimer()
If Not TimerOn Then
TimerId = SetTimer(0, 0, 0.01, AddressOf TimerProc)
TimerOn = True
Else
MsgBox "Timer already On !", vbInformation
End If
End Sub
Sub TimerProc()
On Error Resume Next
GetCursorPos lngCurPos
Set newRange = ActiveWindow.RangeFromPoint(lngCurPos.x, lngCurPos.Y)
If newRange.Address <> oldRange.Address Then Range("A1").Value = newRange.Address
Set oldRange = newRange
End Sub
Sub StopTimer()
If TimerOn Then
KillTimer 0, TimerId
TimerOn = False
Else
MsgBox "Timer already Off", vbInformation
End If
End Sub
In the worksheet module:
Option Explicit
Dim TrgtColor As Long
Dim oldTarget As Range
Private Sub Worksheet_Change(ByVal Target As Range)
Target.Interior.ColorIndex = TrgtColor
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set oldTarget = Target
TrgtColor = oldColor
End Sub
To activate the code, run the "StartTimer" macro and to stop it, run the "StopTimer" macro.
Thanks again to Jaafar, aka screen name rafaaj2000.