VBA – How to implement workbook events in Excel – GIF

The Excel Workbook object is a part of the Workbooks collection. The events are the code, which is “activated” or “triggered”, once a specific event happens. These events are different and could be unlimited, but there are a few built-in Workbook events. These are available through the VBEditor here:

Some of the built-in events, take a parameter. Like the Workbook_NewSheet(ByVal Sh as Object), which takes the newly created sheet as a parameter. Thus, if we would like to change the name of the newly created sheet, the sh object, has a property .Name, which can be obtained through the parameter given. The video below makes sure that every newly added worksheet has a unique name with one line – Sh.Name = Application.UserName + Replace(Time,":","").

  • Application.UserName returns the Username of the application;
  • Time returns the current time;
  • Replace() is needed to replace the “:”, coming from the time;
Tagged with: , , ,