| >GRSoftware >VBA Tutor >Newsletter Tutorials >Tutorial 1 >Tutorial 3 |
VBA TUTOR NEWSLETTER ~ TUTORIAL TWO: Improving the standard Menu Items. |
In this project we will create a macro based on a recorded standard Menu Item, with the code altered to improve its function.
Sub Macro1() Selection.Font.Size = Selection.Font.Size + 1 End Sub NOW ... as in tutorial one, we are first going to get the user to enter some information before the action is carried out. What we are going to ask for is amount to grow the font, to replace the default of '1 Pt'. The completed code should look like ... Sub Macro1() Dim Message, Title, Default, growSize, checkNum GetSize: Message = "Enter a positive value to increase the font size by." Title = "Grow Font Size" Default = 1 growSize = InputBox(Message, Title, Default) checkNum = IsNumeric(growSize) If checkNum Then Selection.Font.Size = Selection.Font.Size + Abs(growSize) Else GoTo GetSize End If End Sub Note the checking done to ensure that only a positive number is passed as the 'growSize' variable. It is first checked by the 'IsNumeric' inbuilt function. If is is not a number, 'checkNum' will be false and the procedure will loop back to again ask for a positive number using the 'goto' statement. If 'growSize' is a number, the 'Abs()' function is used to ensure that only a positive number is passed. NOW ... To use 'Macro1' as the default "Grow Font 1 Pt" Menu Item, it must be renamed to the same name as the standard "Grow Font 1 Pt", which is "GrowFontOnePoint". [ REMEMBER, to find the name of any Menu Item, use the procedure oulined in Issue 1 of this newsletter.] Go and test the NEW "Grow Font 1 Pt" Menu Item. I think you will agree that it is a improvement. NOTE: Just delete the new macro to restore the "standard" Menu Item. YOU HAVE NOW LEARNT HOW EASY IT IS TO IMPROVE THE STANDARD MENU ITEMS. |
HOPEFULLY, THESE CODE EXAMPLES WILL ADD TO YOUR ABILITY TO BECOME A VBA POWER USER! |
| © 2000 Gary Radley |