Home » excel » vba – Deactivate Excel add-in outside business hours

vba – Deactivate Excel add-in outside business hours

Posted by: admin May 14, 2020 Leave a comment

Questions:

I use an Add-in to export all vba code to a repository whenever a workbook is opened. It works great for versioning, but I must remember to deactivate it before I leave the office every evening. If I don’t, the automated Excel processes that run overnight generate the prompt and can’t proceed until the dialog is cleared (i.e. export code…Yes/No?). When I log in, I’m staring at several Excel dialogs waiting for a response.

Note that I don’t always want to export the code…sometimes I just want to open the file and check something, so the Yes/No dialog is required.

How would one automatically deactivate the Add-in every night at a set time before the automated processes start running? Note that manually reactivating it in the morning is not a problem.

If automated Add-in deactivation is not possible, automatically answering the dialog with a “No” during the overnight hours might be an option. Is there a way to automatically click the No button of a MsgBox?

Thanks in advance!

How to&Answers:

Try putting two IF statements around all of your code like this:

Sub test()


If TimeValue(Now) < TimeValue("06:00 PM") Then
    If TimeValue(Now) < TimeValue("08:00 AM") Then
    'Original Code Here
    Else: Exit Sub
    End If
Else: Exit Sub

End If

End Sub

This will automatically exit the sub if it is later than 6:00 PM or earlier than 8:00 AM.

If you want to be able to have the flexibility to choose to execute your code after hours you can do this:

Sub test()

ShowDurationSecs = 5

Dim Rslt As Integer

If TimeValue(Now) < TimeValue("06:00 PM") Then
    If TimeValue(Now) < TimeValue("08:00 AM") Then
        Rslt = CreateObject("WScript.Shell").PopUp("Move Code to Repository?", ShowDurationSecs, "Message Title", 4 + 32)
        If Rslt = 6 Then
        'Original Code Here
        Else: Exit Sub
        End If
    Else: Exit Sub
    End If
Else: Exit Sub

End If

End Sub

This code creates a dialogue box that is on a timer. If the box times out or the user clicks ‘No’, then Rslt will not equal 6 and the code will not execute. If it the user clicks ‘Yes’, Rslt will equal 6 and the code will execute. The pop up box will only show when it is between those hours. If the dialogue box is not that much of an inconvenience, you could get rid of the time conditions and require the user to answer ‘Yes’ every time in order to run the code. Whatever works better for your process.

Answer:

Jarom’s suggestions led to this solution (Thanks Jarom!). The conditionals were reworked a bit using booleans:

Sub test()

Dim Rslt As Integer

ShowDurationSecs = 5

'if after hours, then pop up timed-dialog and acquire Rslt value (actively or passively)

If TimeValue(Now) < TimeValue("07:00 AM") And TimeValue(Now) > TimeValue("07:00 PM") Then Rslt = CreateObject("WScript.Shell").PopUp("Do you want to save the VBA code in this workbook to the git repository?", ShowDurationSecs, "Save Code to Repository", 4 + 32)

If (TimeValue(Now) > TimeValue("07:00 AM") And TimeValue(Now) < TimeValue("07:00 PM")) Or Rslt = 6 Then 'if business hours OR Rslt = 6
'run code export routine
Else: Exit Sub
End If

End Sub

+1 to Jarom for the timed pop-up idea…I’d never used that technique before and will certainly use it again.