Lesson#187: VBA program to shutdown PC
- Go to the Developer Tab in Excel. If you haven’t activated the Developer Tab then see here how to do it.
- Insert a Command Button by clicking on ‘Insert’.
- Create a button on the sheet and double-click to enter the code.
Here is an example of a VBA program that you can use to shut down a Windows PC when a button is clicked:
Private Sub cmdShutdown_Click()
' Shut down the PC
Shell "shutdown -s -t 0"
End Sub
This program uses the Shell
function to execute the shutdown
command with the -s
and -t 0
arguments. The -s
the argument specifies that the PC should be shut down, and the -t 0
argument indicates that the operation should be carried out immediately.
To use this program, create a button on a worksheet and assign this code to the button’s Click
event. When you click the button, it will shut down the PC.
Keep in mind that this program should be used with caution, as shutting down the PC can result in data loss or corruption if the PC is not shut down properly. It’s always a good idea to save your work and close all open programs before shutting down the PC.
Leave a Reply