Microsoft Office TrainingThe UK's Number 1 for Microsoft Office Training Sitemap add this page to your favourites/bookmarksBookmark page
 
view a printable version of this pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

articleTrigger Happy In Excel VBA

You've spent hours writing or recording Macros that perform those repetitive and humdrum tasks in your spreadsheet. But have you given any thought on how your new VBA code will be triggered? How will the code be executed? Will the user initiate the code or some event will trigger the code to execute automatically? This article explores the options available in Excel 2007.

There are six methods to trigger VBA code:
1. Short cut Key.

2. Macro Dialog Box

3. Quick Access Toolbar Icon.

4. Worksheet Button or shape.

5. Custom Ribbon.

6. Event Procedure.

If you have recorded your Macro, you are asked to assign the keyboard shortcut combination when naming the Macro prior to recording. If you have written your VBA code directly into the Visual Basic Editor (VBE) you can assign a keyboard shortcut by clicking the Macros button in the 'Code' group on the Developer tab. Here, all Sub-procedures of Public scope are listed. Select the name of the procedure and click the 'Options' button. Enter the key combination for the selected procedure.

All Public Procedures listed in the Macros dialog box can be executed by selecting the procedure to execute and clicking 'Run'. Scope means if the procedure can be called from other procedures. All procedures stored in global modules are implicitly Public unless they begin with the words 'Private Sub', in which case, they will not show up in the Macro dialog box.

An alternative method to trigger a procedure is to add an icon on to the Quick Access Toolbar. Click the down-arrow to the right of the Quick Access Toolbar and select 'More Commands'. In the 'Choose commands from' box select 'Macros'. From the list of available procedures, select the procedure to add to the toolbar and click 'Add'. To change the appearance of the button, click the 'Modify' button. In the displayed dialog box, select an alternative icon and modify the procedure's display name. (Note: You should only add a macro to the Quick Access Toolbar which has been saved in the 'Personal Macro Workbook'.

You can also add a button or a shape in the body of the worksheet and assign a procedure to it. On the Developer tab, click the 'Insert' button in the 'Controls' group. Select the 'Button' shape and click in the worksheet. A button is added and the 'Assign Macro' dialog is shown. Select the required procedure to assign to the button and click 'OK'. To change the caption of the button, right click over the button and select 'Edit Text'. Alternatively, select a shape from the 'Shapes' icon in the 'illustrations' group on the Insert tab. Right click over the inserted shape and select 'Assign Macro'. Select the required procedure to assign to the button and click 'OK'.

In previous versions of Microsoft Excel, it was possible to create custom Toolbars and custom Menus and add macros to these objects. In Excel 2007 you can customise the Ribbon by adding custom tabs, groups and buttons.
This is done by inserting an XML part that defines the custom user interface (UI) into the Excel XML workbook. (The file extension for a Macro Enabled Workbook is xlsm). To do this requires some in-depth XML knowledge however with a bit of help from the 'Custom UI Editor Tool', it not as hard as it might seem.

1. Download and install the Custom UI Editor Tool from http://openxmldeveloper.org/attachment/808.ashx

2. Create /record the macros in a workbook saving it as a Macro Enabled workbook. Close the workbook

3. Run the newly installed Microsoft Office 2007 Custom UI Editor

4. From the File menu in the UI Editor, select 'Open' and navigate to the aforementioned workbook with your VBA code.

5. From the Sample menu in the UI Editor select 'Custom Tab'.

6. The basic XML is shown for one tab with the label 'Custom Tab', one group with the label 'Custom Group' and one button labelled 'Custom Button'. Each of these labels can be changed as required.

7. The argument imageMso="HappyFace" sets the image for the button. You can download a workbook from Microsoft which adds a group to the developer tab with icon galleries of built-in icons available for specifying the imageMso value. http://www.microsoft.com/downloads/details.aspx?familyid=12B99325-93E8-4ED4-8385-74D0F7661318 or search for 2007 Office System Add-In: Icons Gallery.

8. When the button is clicked, we will want a VBA procedure called 'WelcomeMessage' to run. For this to happen, modify the XML code to show onAction="WelcomeMessage".

9. To create the VBA needed to run when the button is clicked, select the 'Generate Callback's icon on the UI Editor's toolbar.

10. Copy all the VBA code shown.

11. Open the workbook again and paste the VBA code in a existing or new module in the VBA Editor.

12. Enter your VBA code between the Sub & End sub lines. (You may choose to call an existing sub routine.

13. In the workbook, you should now have a new tab, with a new group and a button.

An Event Procedure is a VBA procedure that is automatically executed in response to a specific event initiated by the user or the system. For example, every time the workbook is opened, closed, saved etc. The Event Procedures are stored in the module of the object where the event occurs. For example, the aforementioned events are all Workbook events and will be stored in the 'ThisWorkbook' module. Other events like Change, BeforeRightClick and calculate are stored in the relevant Sheet module (Sheet1, Sheet2 etc). Each event must also be named in a specific way.

If we want a VBA procedure to run when the workbook is opened:

1. In the VBA Editor, double click the 'ThisWorkbook' module to open it.

2. Select the Object drop down box at the top left of the code window (will show 'General' by default) and select Workbook.

3. From the Procedure dropdown box at the top right of the code window, select the event required, e.g. 'Open'. A new procedure called Workbook_Open is created and the VBA code for this event should be entered between the procedure name and the 'End Sub' statement

Conclusion: VBA procedures, written or recorded can be triggered by a wide range of methods by the user or automatically by the system and by other VBA code.

Author is a freelance copywriter. For more information on vba excel 2007 course, please visit http://www.microsofttraining.net


Original article appears here:
http://www.microsofttraining.net/article-588-trigger-happy-in-excel-vba.html


Back to article list

Distribution notes

PUBLICATION GUIDELINES

  • You have permission to publish this article for free providing the "About the Author" box is included in its entirety.
  • Do not post/reprint this article in any site or publication that contains hate, violence, porn, warez, or supports illegal activity.
  • Do not use this article in violation of the US CAN-SPAM Act. If sent by email, this article must be delivered to opt-in subscribers only.
  • If you publish this article in a format that supports linking, please ensure that all URLs and email addresses are active links, without the rel='nofollow' tag.
  • Software Training London Ltd. owns this article. Please respect the author's copyright and above publication guidelines.
  • If you do not agree to these terms, please do not use this article.

Rate this page:
2.8/5 (129 votes cast)
Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses
Microsoft Certified Partner
Security Seal verified by visa, mastercard securecard

Mini sitemap. These are the main areas of our web site. Full sitemap.

Management training

Professional Skills courses
Project Management Course London
Project Management Courses London
Project Management Training London
Project Management Training
Project Seminar
Project Seminars
Time Management Course London
Time Management London
Time Management Courses London
Time Management Training London
Introduction to Finance course
Assertiveness Skills course
Effective Communications Skills training
Presentation Skills London

Training Formats

Public scheduled courses
On-site training
Closed company courses

Consultancy
Application Development

Blogs

Excel Training
MS Project Training
Microsoft Training Blog

Version differences

Office 2010 vs 2007
MS Project version differences

Training Information

London Computer Training
Computer Training London
Docklands Training Courses
Docklands Training London

Training venues London
Client list
FAQ
Pricing and availability
Course details / Syllabus

Training Articles
Training Information

Microsoft training

Microsoft Office training
& IT Applications

Microsoft Project training
Microsoft Outlook training
Microsoft Powerpoint training
Microsoft Word training
MS Project courses
MS Project training
Outlook courses
PowerPoint courses
PowerPoint training
VBA courses
Word courses
Microsoft.training
(more...)

Excel Training

Excel courses
Excel Training Courses Medway
Autonumber in Excel
Microsoft Excel training
Basic Excel Courses
Basic Excel Course
Basic Excel Training

Interested in MS Access training?

Access courses
Microsoft Access training
Microsoft access courses
Microsoft training access course
Microsoft+access+training
Access courses in london

Training provider

Training providers
IT training companies
IT training providers
Management Training providers
Management Training provider

Event history, feedback results
Events in 2012 · 2011 · 2010 · More

See also

Crystal Reports training