VBA Macro to have the mouse move once a day to keep screen awake?

BAQI

New Member
Joined
Dec 2, 2022
Messages
40
Office Version
  1. 2016
Platform
  1. Windows
I have an excel spreadsheet up on a computer all day long, and every morning if I don't remember to move the mouse, the computer will logout.

Is there a way to use a VBA macro to have the mouse move automatically once a day (at a set time would be better), so that the computer doesn't log itself out?

It's a shared excel spreadsheet, so the macro can't interfere with other people who use it.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
try below

VBA Code:
Public MoveMouse As Integer
Public Toggle As Integer

Private DtmNext As Date
Private Type POINTAPI
    xCord As Long
    yCord As Long
End Type

Private Declare PtrSafe Function GetCursorPos Lib "user32" (Point As POINTAPI) As Long
Private Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Long

Sub Move_Cursor()
Dim Hold As POINTAPI
    GetCursorPos Hold
    If Toggle Mod 2 = 0 Then
        SetCursorPos Hold.xCord + 20, Hold.yCord
        Toggle = 1
    Else
        SetCursorPos Hold.xCord - 20, Hold.yCord
        Toggle = 2
    End If
End Sub

Sub Start_Cursor()
Dim PauseTime, Start

Do While MoveMouse = 1
    PauseTime = 60    ' Set duration.
    Start = Timer    ' Set start time.
    Do While Timer < Start + PauseTime
        DoEvents    ' Yield to other processes.
    Loop
    Move_Cursor
Loop

End Sub

Sub Stop_Cursor()
    MoveMouse = 0
End Sub

Set 2 buttons on a a sheet and add to that sheet code
Code:
Private Sub CommandButton1_Click()
MoveMouse = 1
Cnt = 0
Toggle = 2
Start_Cursor
End Sub

Private Sub CommandButton2_Click()
Stop_Cursor
End Sub
 
Upvote 0
If you can install small apps then there are a lot of small windows apps that move the mouse at set times. Microsoft even make their own one. Some of them have the option to hide the cursor when it moves so you don't see it move.
 
Upvote 0
Just a note, the time used in the code is in seconds so to be inside a day it would need 86,399
 
Upvote 0
try below

VBA Code:
Public MoveMouse As Integer
Public Toggle As Integer

Private DtmNext As Date
Private Type POINTAPI
    xCord As Long
    yCord As Long
End Type

Private Declare PtrSafe Function GetCursorPos Lib "user32" (Point As POINTAPI) As Long
Private Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Long

Sub Move_Cursor()
Dim Hold As POINTAPI
    GetCursorPos Hold
    If Toggle Mod 2 = 0 Then
        SetCursorPos Hold.xCord + 20, Hold.yCord
        Toggle = 1
    Else
        SetCursorPos Hold.xCord - 20, Hold.yCord
        Toggle = 2
    End If
End Sub

Sub Start_Cursor()
Dim PauseTime, Start

Do While MoveMouse = 1
    PauseTime = 60    ' Set duration.
    Start = Timer    ' Set start time.
    Do While Timer < Start + PauseTime
        DoEvents    ' Yield to other processes.
    Loop
    Move_Cursor
Loop

End Sub

Sub Stop_Cursor()
    MoveMouse = 0
End Sub

Set 2 buttons on a a sheet and add to that sheet code
Code:
Private Sub CommandButton1_Click()
MoveMouse = 1
Cnt = 0
Toggle = 2
Start_Cursor
End Sub

Private Sub CommandButton2_Click()
Stop_Cursor
End Sub

I'm new to VBA/macros as a whole, so I'm sorry if I'm overlooking something obvious.

I created a new spreadsheet to run it, as it wasn't running on my main one. It still isn't, or doesn't appear to be. I put the time at 10 seconds to see if it would work. I also have macros enabled on this sheet.

Once this is running, is there code I can add to have the macro run as soon as the spreadsheet is opened?

Thank you for your help with this.
 

Attachments

  • Screenshot 2023-04-13 062635.png
    Screenshot 2023-04-13 062635.png
    42.6 KB · Views: 59
  • Screenshot 2023-04-13 062719.png
    Screenshot 2023-04-13 062719.png
    97.9 KB · Views: 58
  • Screenshot 2023-04-13 062742.png
    Screenshot 2023-04-13 062742.png
    68.3 KB · Views: 57
  • Screenshot 2023-04-13 063042.png
    Screenshot 2023-04-13 063042.png
    24 KB · Views: 57
Upvote 0
This code would need be put in ThisWorkbook
1681382459383.png


VBA Code:
Private Sub Workbook_Open()
MoveMouse = 1
Cnt = 0
Toggle = 2
Start_Cursor
End Sub
 
Upvote 0
This code would need be put in ThisWorkbook
View attachment 89598

VBA Code:
Private Sub Workbook_Open()
MoveMouse = 1
Cnt = 0
Toggle = 2
Start_Cursor
End Sub
Okay, just to make sure we're on the same page.

In your first response, you gave two different codes. The first one goes under "ThisWorkbook" and the second goes under "Sheet1", correct? I'm still trying to get the original macro to work.

When I compile the code it gives the error in the attached screenshot.
 

Attachments

  • Screenshot 2023-04-13 072314.png
    Screenshot 2023-04-13 072314.png
    32.5 KB · Views: 28
Upvote 0
In my 1st response there were 2 sets. One lot went in to a module the others (for the buttons) went on the sheet concerned. So to get the cursor to start moving when you open the workbook you need to copy the code for start cursor into Workbool_Open. It will then run as soon as the workbook is open. I don' think you need to copy any code to workbook_Close as the routine stops when the workbook is closed any way.
 
Upvote 0
In my 1st response there were 2 sets. One lot went in to a module the others (for the buttons) went on the sheet concerned. So to get the cursor to start moving when you open the workbook you need to copy the code for start cursor into Workbool_Open. It will then run as soon as the workbook is open. I don' think you need to copy any code to workbook_Close as the routine stops when the workbook is closed any way.
Okay, I got it to work. Thank you so much for all the help.

If you wouldn't mind, hopefully one last question.

How the spreadsheet works is that it's always open on one monitor, and it is a shared excel spreadsheet. Throughout the day, people open it up from their separate computer, edit it, and save it which then updates the main computer/monitor. It isn't closed out of on the main monitor when this happens. It just updates the changes that were made from the other computer.

This code will be able to work in that situation?

I really can't thank you enough.
 
Upvote 0
In my 1st response there were 2 sets. One lot went in to a module the others (for the buttons) went on the sheet concerned. So to get the cursor to start moving when you open the workbook you need to copy the code for start cursor into Workbool_Open. It will then run as soon as the workbook is open. I don' think you need to copy any code to workbook_Close as the routine stops when the workbook is closed any way.
Well, the macro does work, but it doesn't prevent the monitor from going to sleep. Back to the drawing board! I appreciate the help though.
 
Upvote 0

Forum statistics

Threads
1,216,210
Messages
6,129,525
Members
449,515
Latest member
lukaderanged

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