While mouse cursor on top of A1 cell

Erdinç E. Karaçam

Board Regular
Joined
Sep 23, 2006
Messages
202
Hi everyone,

I would like to change A1 cell's Interior.ColorIndex to red color and A1's font format to Italic, while mouse cursor on top of A1 cell.

Can i do it with a VBA code or any different way to do it?

Thanks a lot.


:-D For a funny example:

Code:
Sub CursorOnA1() 
    If MouseCursor OnTopOf [A1] Then 
        With [A1] 
            .Interior.ColorIndex = 3 
            .Font.Italic = True 
        End If 
    End If 
End Sub
 
Calling of the start of the mouse event in the Module, the "access error to route or file" appear. It seems that when passing the sheet object or when it is processed, the access to the sheet object or other component can't be achieved.
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Calling of the start of the mouse event in the Module, the "access error to route or file" appear. It seems that when passing the sheet object or when it is processed, the access to the sheet object or other component can't be achieved.

Add a the lines in red to the SetUpVBSFile Routine as follows and see if solves the problem.

Code:
Private Sub SetUpVBSFile()
 
    Dim lProcID As Long
    
    'create a background vbs file on the fly.
    Open MOUSE_WATCHER_VBS For Output As #1
    Print #1, "set wb=Getobject(" & Chr(34) & Me.FullName & Chr(34) & ")"
    Print #1, "On Error Resume Next"
    Print #1, "Do"
    Print #1, "wb.Application.Run(" & Chr(34) & Me.Name & _
    "!StartMouseMoveEvent" & Chr(34) & ")"
    Print #1, "Loop"
    Close #1
    
    [COLOR=red]Do
        DoEvents
    Loop Until Len(Dir(MOUSE_WATCHER_VBS)) <> 0
[/COLOR]    
    'execute the background vbs file.
    lProcID = Shell("WScript.exe " & MOUSE_WATCHER_VBS)
    
    'store the exe PID in a window to persist
    'even when the project is reset.
    'will be needed to terminate the process later.
    SetProp GetDesktopWindow, "lProcID", lProcID
 
End Sub
 
Upvote 0
Good morning:
Tryied the code added, but still same error message. Error message, as said before, appeared marked by debugger in highlighted line.


Sub StartMouseMoveEvent()

Call ThisWorkbook.StartMouseWatcher(ByVal Sheets(1))

End Sub

Message error still the same, "Access error to the route or the file".

Thanks for your kind help.






 
Upvote 0
Good morning:
Ok. Solved. The issues are 2:

1.- File and route in the instruction Open from excel vb code must be written with complete name, but same route and file name for vbscript must be written with 8 characters limit, as they appear in DOS with the ~ character, else the calling of the script will not happen correctly
2.- Writing the VBScript, the getobject call must be done like this to result:
set wb= getobject( , "Excel.Application")

It will take the already created instance of Excel and perform the next secuence of commands. Otherwise the script dump an error. At least it worked for me as espected.
Thank you very much for this wonderfull code, very impressive.
 
Upvote 0
Good morning:
Ok. Solved. The issues are 2:

1.- File and route in the instruction Open from excel vb code must be written with complete name, but same route and file name for vbscript must be written with 8 characters limit, as they appear in DOS with the ~ character, else the calling of the script will not happen correctly
2.- Writing the VBScript, the getobject call must be done like this to result:
set wb= getobject( , "Excel.Application")

It will take the already created instance of Excel and perform the next secuence of commands. Otherwise the script dump an error. At least it worked for me as espected.
Thank you very much for this wonderfull code, very impressive.

Glad you made it work and Thanks for the addition.
 
Upvote 0
Hmm - I wonder if this could be modified for a mousemove event over an image?

I'm currently using
Code:
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
   Const lngSensitivity As Long = 1
   With ActiveSheet.Image1
     .Top = Range("H4").Top
        .Left = Range("H4").Left
            .Width = Range("H4:I8").Width
              .Height = Range("H4:I8").Height
                
      Me.Shapes("Auto").Visible = _
      (X > lngSensitivity And X < .Width - lngSensitivity And _
      Y > lngSensitivity And Y < .Height - lngSensitivity)
   End With

End Sub

however this code is very unreliable. Often times when you mouse over the image the shape will stay visible when you remove the cursor. Then when you move the cursor back over the image the shape will disappear. I've tried to over come this by add a shape.visible = false item, which works but only when another event occurs. Thoughts?

Thanks!
 
Upvote 0
Hmm - I wonder if this could be modified for a mousemove event over an image?

I'm currently using
Code:
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
   Const lngSensitivity As Long = 1
   With ActiveSheet.Image1
     .Top = Range("H4").Top
        .Left = Range("H4").Left
            .Width = Range("H4:I8").Width
              .Height = Range("H4:I8").Height
                
      Me.Shapes("Auto").Visible = _
      (X > lngSensitivity And X < .Width - lngSensitivity And _
      Y > lngSensitivity And Y < .Height - lngSensitivity)
   End With

End Sub
however this code is very unreliable. Often times when you mouse over the image the shape will stay visible when you remove the cursor. Then when you move the cursor back over the image the shape will disappear. I've tried to over come this by add a shape.visible = false item, which works but only when another event occurs. Thoughts?

Thanks!


how about putting the image inside a hidden comment so when you move the mouse pointer over the cell the comment pops up with the picture .
 
Upvote 0
Hmm - I wonder if this could be modified for a mousemove event over an image?

however this code is very unreliable. Often times when you mouse over the image the shape will stay visible when you remove the cursor. Then when you move the cursor back over the image the shape will disappear. I've tried to over come this by add a shape.visible = false item, which works but only when another event occurs. Thoughts?

Thanks!

The following should show the image when you move the mouse pointer over the range H4:I8 and hide it if the pointer is moved away from the range. worked smoothly when tested.


Workbook demo (the workbook contains a hidden image control over the range H4:I8 on sheet1)

Code in the Workbook module :

Code:
Option Explicit

Public Event MouseMove(ByVal Target As Range)

Private Type POINTAPI
    x As Long
    y As Long
End Type

Private Declare Function GetProp Lib "user32" _
Alias "GetPropA" _
(ByVal hwnd As Long, _
ByVal lpString As String) As Long
 
Private Declare Function SetProp Lib "user32" _
Alias "SetPropA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal hData As Long) As Long
 
Private Declare Function RemoveProp Lib "user32" _
Alias "RemovePropA" _
(ByVal hwnd As Long, _
ByVal lpString As String) As Long

Private Declare Function GetDesktopWindow Lib _
"user32.dll" () As Long

Private Declare Function GetCursorPos Lib "user32.dll" _
(ByRef lpPoint As POINTAPI) As Long

Private Declare Function OpenProcess Lib "Kernel32.dll" _
(ByVal dwDesiredAccessas As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcId As Long) As Long

Private Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Private Const PROCESS_TERMINATE As Long = &H1

Private Const MOUSE_WATCHER_VBS As String _
= "C:\MouseWatcher.vbs"


Private Sub Workbook_Open()

    Call StartMouseWatcher(Sheets(1))
    
End Sub


Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Call StopMouseWatcher
    
End Sub


Private Sub StartMouseWatcher(ByVal Sh As Worksheet)

    Dim tPt As POINTAPI
    Dim oTargetRange As Range
    
    'don't run the VBSript more than once.
    If GetProp(GetDesktopWindow, "lProcID") = 0 Then
        Call SetUpVBSFile
    End If
    
    'hook the target sheet.
     CallByName Sh, "Worksheet", VbSet, ThisWorkbook
     
    'get the current cursor pos.
    GetCursorPos tPt
    
    'store the range under the mouse pointer.
    On Error Resume Next
        Set oTargetRange = ActiveWindow.RangeFromPoint(tPt.x, tPt.y)
    On Error GoTo 0
    
    'ignore non range objects and other sheets.
    If TypeName(oTargetRange) <> "Range" Or _
    Not ActiveSheet Is Sh Then Exit Sub
    
    'pass the range to the MouseMove event.
    RaiseEvent MouseMove(ByVal oTargetRange)

End Sub


Private Sub StopMouseWatcher()

    Dim hProcHandle As Long
    Dim oTragetRange As Range

    'kill the VBScript exe.
    hProcHandle = OpenProcess(PROCESS_TERMINATE, 0, _
    GetProp(GetDesktopWindow, "lProcID"))

    TerminateProcess hProcHandle, 1
    CloseHandle hProcHandle
    'cleanup.
    RemoveProp GetDesktopWindow, "lProcID"
    'delete the temp vbs file.
    On Error Resume Next
        Kill MOUSE_WATCHER_VBS
    On Error GoTo 0
    
End Sub


Private Sub SetUpVBSFile()

    Dim lProcID As Long
    
    'create a background vbs file on the fly.
    Open MOUSE_WATCHER_VBS For Output As #1
    Print #1, "On Error Resume Next"
    Print #1, "set wb=Getobject(" & Chr(34) & Me.FullName & Chr(34) & ")"
    Print #1, "Do"
    Print #1, "wb.Watch"
    Print #1, "Loop"
    Print #1, "Set wb=Nothing"
    Close #1
    
    Do
        DoEvents
    Loop Until Len(Dir(MOUSE_WATCHER_VBS)) <> 0

    'execute the background vbs file.
    lProcID = Shell("WScript.exe " & MOUSE_WATCHER_VBS)
    
    'store the exe PID in a window to persist
    'even when the project is reset.
    'will be needed to terminate the process later.
    SetProp GetDesktopWindow, "lProcID", lProcID

End Sub


'Only Public Method accessed by the VBScript.
'============================================
Public Sub Watch()
    Call StartMouseWatcher(ByVal Sheets(1))
End Sub
Code in the worksheet module :

Code:
Option Explicit

Public WithEvents Worksheet As ThisWorkbook

'==========================================
Private Sub Worksheet_MouseMove(ByVal Target As Range)
    
    On Error Resume Next

    If Union(Target, Range("H4:I8")).Address = Range("H4:I8").Address Then
        Image1.Visible = True
    ElseIf Image1.Visible Then
        Image1.Visible = False
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top