DEV Community

Andrews
Andrews

Posted on • Originally published at geekexcel.com on

How to Run a Macro Automatically When Workbook Opens in Excel?

In this article, you will learn how to run a Macro automatically when Workbook opens in Excel Office 365. Let’s see them below!! Get the official version of MS Excel from the following link: https://www.microsoft.com/en-in/microsoft-365/excel

Auto Run Macro When Workbook Closes

  • To run macro as the workbook opens , you have to use Workbook Event Workbook_Open.
  • The *syntax * of this event is given below.
Private Sub Workbook_Open()

 'Your Code here.

End Sub

Enter fullscreen mode Exit fullscreen mode
  • Firstly, in the Excel Worksheet, you have to go to the Developer Tab.
  • Then, you need to ** ** select the Visual Basic option under the Code section.

Select Visual Basic

  • Now, in project explorer , you have to find ThisWorkbook object in project explorer and double-click on it.

Find ThisWorkbook object

  • After that, you have to copy and paste the code given below.
 Private Sub Workbook_Open()ps = 12345

pw = InputBox("Please enter the password.") + 0

If pw = ps Then

 MsgBox ("Welcome Sir!")

Else

 MsgBox ("Goodbye")

 ThisWorkbook.Close

End If

End Sub

Enter fullscreen mode Exit fullscreen mode
  • You need to save the code by selecting it and then close the window.

Save the code

  • Again, you have to go to the Excel Spreadsheet , and click on the Developer Tab.
  • You need to choose the Macros option in the Code section.

Choose Macro option

  • Then, you have to Save the workbook and run the code once.
  • Now, you need to Close the workbook and try to open it.
  • After that, the code will run as soon as the workbook opens , and you have to enter the password.

  • Finally, you will receive the message box with Welcome Sri.

Final output

Wind-Up

We hope that this short tutorial gives you guidelines on how to run a Macro automatically when Workbook opens in Excel Office 365. ** Please leave a comment in case of any **queries, and don’t forget to mention your valuable suggestions as well. Thank you so much for Visiting Our Site!! Continue learning on Geek Excel!! *Read more on Excel Formulas *!!

Read Next:

Top comments (0)