# Deactivate active cell in VB



## Sean15 (Dec 14, 2022)

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.


```
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
```


----------



## Zot (Dec 14, 2022)

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.


----------



## Sean15 (Dec 16, 2022)

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?


----------



## Zot (Dec 18, 2022)

I tried not hitting Esc button after copy paste and the routine still trigger save prompt. Maybe I am still on Excel 2016?


----------



## Michael M (Dec 18, 2022)

I'd put the line of code as the first line of code in the first macro that runs.


----------



## Zot (Dec 18, 2022)

Michael M said:


> I'd put the line of code as the first line of code in the first macro that runs.


I tried that too but with or without it it still worked. So, I can't conclude 😁


----------



## Sean15 (Dec 19, 2022)

Thanks. I added command line to 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?


----------

