| >GRSoftware >VBA Tutor >Newsletter Tutorials >Tutorial 13 >Tutorial 15 |
VBA TUTOR NEWSLETTER ~ TUTORIAL FOURTEEN: CREATING AND USING AN 'XLA' ADD-IN FILE. |
For advanced users wanting to distribute their project(s), 'xla' add-ins are the ideal solution for storing and distributing complete applications, or custom functions and utilities. STEP ONE: Introduction to 'Add-in's' A Microsoft Excel add-in is a workbook with an .xla extension. They are installed by default in the "\Program Files\Microsoft Office\Office\Library folder". Loading an add-in makes the feature available in Excel and adds any associated commands to the appropriate menus. STEP TWO: Creating the Code for the 'Add-In' The first task is to create the 'AddinInstall()' and 'AddinUnnstall()' events. The install event adds a menu item in the 'Format' menu. The uninstall event deletes the menu item from the 'Format' menu. If you have trouble following the code, use the help in the VB Editor for reference to the 'objects' ( eg. 'CommandBars' ) and 'methods' ( eg. 'add' ) used. In the project window of the VB Editor, enter this following code into the 'ThisWorkbook' module. Option Explicit Private Sub Workbook_AddinInstall() Dim objCmdBrPp As CommandBarPopup Dim objCmdBtn As CommandBarButton Set objCmdBrPp = Application.CommandBars ("Worksheet Menu Bar").Controls("Format") On Error Resume Next Set objCmdBtn = objCmdBrPp.Controls.Add (Type:=msoControlButton, before:=4) With objCmdBtn .Caption = "&Favorite Date Format" .OnAction = "AddInCode.favDateFormat" End With Set objCmdBrPp = Nothing Set objCmdBtn = Nothing End Sub Private Sub Workbook_AddinUninstall() Dim Count As Integer On Error Resume Next For Count = 1 To Application.CommandBars("Format").Controls.Count If Application.CommandBars("Format").Controls(Count).Caption = "&Favorite Date Format" Then Application.CommandBars("Format").Controls(Count).Delete Exit Sub End If Next Count End Sub The procedure to be run must reside in a separate 'AddInCode' module.
Public Sub favdateFormat() If Selection Is Nothing Then Exit Sub End If Selection.NumberFormat = "dd-mm-yy" End Sub
NOTE: NOTE: NOTE: NOTE: NOTE: NOTE: NOTE: NOTE: Don't feel like copying the code or having trouble with the code, then download 'favDate.xla'. This tutorial was based upon a more complex Microsoft Developer Network tutorial that can be found @ MSDN ~ Creating Add-ins in Microsoft Excel 97 |
HOPEFULLY, THIS CODE EXAMPLE WILL ADD TO YOUR ABILITY TO BECOME A VBA POWER USER! |
| © 2000 Gary Radley |