Inserting Events Template

When I want to write event code in a Workbook, I Alt-F11 to open the VBE, Cntl-R to open the Project Explorer, arrow to the class module in which the code will go, and… Then, as far as I can tell, I have to use my mouse and the dropdowns at the top of the module to insert the Sub and End Sub lines.

dropdowns at the top of the sheet module showing Worksheet and Change

I could just type them in, but I would to get the arguments just right or the VBE wouldn’t like it. It would be nice if the Insert > Procedure menu item had a provision for event code.

Insert procedure dialog box

The best solution I could muster is to create text files that contain the code I want. I have two text files in my Office folder (Program FilesMicrosoft Office 2000Office is the default folder for Insert > File) called SheetEvents.txt and WorkbookEvents.txt.

Insert file menu item

SheetEvents contains the Worksheet_Change and Workbook_SelectionChange events. WorkbookEvents contains Open, BeforeClose and BeforeSave. Those are the events I use most often. I thought about making a separate file for each event, but I decided to just put the most common ones in a file and delete anything I don’t need. Creating the text files was a simple copy and paste operation into Notepad after I used the dropdowns to get the events I wanted.

Posted in Uncategorized

10 thoughts on “Inserting Events Template

  1. I hate how when you select an item on the left it automatically inserts what it wants from the right.

    For example when I create a new user form and I choose “UserForm” from the left dropdown it automatically inserts “UserForm_Click”. It would be nice if it would let me pick what I want from the right before inserting anything.

  2. MZ Tools 3.0 for VBA has two functions which might help. The Add Procedure function does not, afaik, add an event procedure, BUT the Code Template allows the user to define pre-defined code segments – and just for Dick, you can define a shortcut for each code segment.
    BTW, I find MZ Tools provides some really useful functions. wouldn’t code without it.

  3. >> I have to use my mouse and the dropdowns at the top of the module

    I don’t know if you’re looking for a keyboard shortcut, Dick, but Ctrl-F2 activates the left dropdown and a Tab will then take you to the right.

  4. Dick –

    I can’t believe you’d add a whole layer of infrastructure (text files with event procedure stubs) just to avoid using the mouse. Though I’m glad Jim saved you from this.

    – Jon

  5. [quote]
    I miss the old TRS-80 days, I guess.

    Yeah, what a hoot it was: 128k and everything you did was a programming process. Kind of like crawling: you were forced to learn to walk; because you were soon leaving the top two inches of your forehead on the bottom of the coffee table… :-D


Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published. Required fields are marked *