Deactivate active cell in VB

Sean15

Well-known Member
Joined
Jun 25, 2005
Messages
719
Office Version
  1. 2010
Platform
  1. Windows
The code below closes file after three minutes of inactivity. But if there is an active cell, the code will not execute. For example, if I copy data from a cell, the cell remains active until I press Esc, at which time the code executes. Can someone update code so it deactivates active cell after 10 seconds, then allowing the three minutes shutdown countdown to execute? Thank you for your help.

VBA Code:
Dim CloseTime As Date

Sub TimeSetting()

CloseTime = Now + TimeValue("00:03:00")

On Error Resume Next

Application.OnTime EarliestTime:=CloseTime, _

Procedure:="SavedAndClose", Schedule:=True

End Sub

Sub TimeStop()

On Error Resume Next

Application.OnTime EarliestTime:=CloseTime, _

Procedure:="SavedAndClose", Schedule:=False

End Sub

Sub SavedAndClose()

ActiveWorkbook.Close Savechanges:=True

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I think what you meant by active cell is actually selected cell where you see hashed border. Normally you press Esc to get rid of it. Using VBA it is equal to command:

Application.CutCopyMode = False

Not sure how your workbook work in relation with the subroutines above to suggest where to put it. :unsure:
 
Upvote 0
Yes, you are correct. What I meant to say is when there is a selected cell, VBA code in post #1 will not execute until I press Esc. I have a bad habit of forgetting to press Esc so I want to automate clear selected cell. OP suggested command:

Application.CutCopyMode = False

I don’t know where to add command - could someone help?
 
Upvote 0
I tried not hitting Esc button after copy paste and the routine still trigger save prompt. Maybe I am still on Excel 2016?
 
Upvote 0
I'd put the line of code as the first line of code in the first macro that runs.
 
Upvote 0
Thanks. I added command line to code:

VBA Code:
Dim CloseTime As Date

Sub TimeSetting()

Application.CutCopyMode = False

CloseTime = Now + TimeValue("00:01:00")

On Error Resume Next

Application.OnTime EarliestTime:=CloseTime, _

Procedure:="SavedAndClose", Schedule:=True

End Sub

Sub TimeStop()

On Error Resume Next

Application.OnTime EarliestTime:=CloseTime, _

Procedure:="SavedAndClose", Schedule:=False

End Sub

Sub SavedAndClose()

ActiveWorkbook.Close Savechanges:=True

End Sub

But I notice something odd:

If I select & copy entire cell, the code executes as required.

But if I select & copy characters inside cell, the code won’t execute until I press ESC.

Could someone advise please?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
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