Excel VBA Shutdown Computer – Visual Basic Code

Note: Save all your unsaved Documents before Testing this Code

This VB6 or VBA Shutdown, Log off or Reboot the computer.  Sometimes, the code does not even ask you for any confirmation to save the unsaved work. Always save your “in progress” work and then test this code.

I myself ended up in rebooting the system with this code multiple times, before I publish this code. Good that I saved all my MS Word, Excel and Notepad documents that I was working on.

Visual Basic / VBA Code – Shutdown Logoff Computer

The actual windows api function used in Excel VBA PC shutdown code is:

Syntax: ExitWindowsEx (option , 0)

The code snippet also works in Visual Basic programing also.

'Visual Basic / Excel VBA code - To Shutdown Computer, Logoff, Reboot
'For More Info search for these Windows Keywords
'ExitWindowsEx Windows API Function to perform the following
'in Computers running with Windows Operating System
'(1) Logs off        - Code: 0
'(2) Shuts down      - Code: 1
'(3) Reboot          - Code: 2
'(4) Forced Process  - Code: 4
'(4) Power Off       - Code: 8
'(5) Force if Hung   - Code: 10

''''API Function Declaration
Public Declare Function ExitWindowsEx Lib "user32" _
    (ByVal dwOptions As Long, _
    ByVal dwReserved As Long) As Long

''''Call the below Functions to Perform Log off or Shutdown or Reboot
Private Sub Excel_VBA_Log_Off_Computer()
    ExitWindowsEx 4, 0
End Sub

Private Sub Excel_VBA_ShutDown_Computer()
    ExitWindowsEx 1, 0
End Sub

Private Sub Excel_VBA_Reboot_Computer()
    ExitWindowsEx 2, 0
End Sub

Note: Add the force flag code to normal process code to force complete an operation.

For example: If you want a normal shutdown use ‘1’. If it is force Shutdown then use ‘5’ i.e., Shutdown Code(1) + Force Complete Code(4).

Steps to Execute the VBA Shutdown Code

  • Create a new workbook and press Alt F11 to view VB Editor.
  • Copy and paste the above code.
  • Save this workbook as Macro Enables workbook and also, save all other unsaved documents.
  • Execute the code by pressing Alt F5. System will start to Log off.

Where can we use this?

  • Save Power: Many times, we leave office just by locking the computer. The electricity is still used up to keep the desktops up and running. You can use this code to set a time limit (example: 10.00 pm or 11.00 pm) in excel and if that time limit is reached, this code can be used to shutdown workstations.
  • Limit Working Hours: Are you so addicted to sit for long time with computer at home. Set a time limit to shutdown and spend some time with Family.

Well, I have given 2 examples. Now use your imagination further to find where this code can be used. If you come up with any interesting usage with this code, post your comments or article link at comments section.

MSDN References:


Related Topics:

  1. How to set Timer and lock a computer with Excel?
  2. How to set timer in Excel to execute any code?

One thought on “Excel VBA – Shutdown Computer – Logoff User – Visual Basic Code”

  1. Thanks for this article! I’m using this code, but only the options for 0, 4, and 10 for logging off work for me. Reboot and shut down option do nothing. Any thoughts? Thanks again.

Leave a Reply