You are on page 1of 9

1. Go to Start>Programs>Microsoft Excel to open the application. Go to File>New and open a new spreadsheet.

Then go to Window>Unhide to bring the personal macro workbook forward, while you work with Excel this macro workbook remains hidden but once you need to edit a macro you need to unhide it to be able to work with it.

2. Select PERSONAL.XLS and click OK to open the personal macro workbook.

3. Now, well access the macros already created. Go to Tools>Macros and select Macros.

4. Now you can see all the macros already created and stored in the personal macro workbook. For the purposes of this tutorial, well need to update the macro called ht, this is the macro that has the names of the training managers recorded. Select ht and click edit to edit the macro.

5. In this step, you get a glimpse of the Microsoft Visual Basic Application. You will notice three distinctive windows: 1. The Project Window located to the upper left hand side, which shows you the files currently open. 2. The Properties Window located right below the Project window to the lower left hand corner, which lists the properties of the macros. 3. The Code window, located to the right hand side of the screen. In the code window is where you edit the actual code of the macros and the one that we will concentrate for the remainder of this tutorial.

Please Note: Before moving forward, we need to go over some basic elements within Visual Basic so that it is easier for you to use this new application. First, Visual Basic is a type of computer language where you use computer code to tell the program what you want it to do. You need to become familiar with the basic elements of the language and its structure. Lets compare, for example, the English language to Visual Basic. The English language is made up of words or nouns, and when these nouns do something, we call it a verb, and when we want to describe a characteristic of a noun, we use adjectives. The syntax of Visual Basic is very similar, for example, in VBA there are things called objects (like nouns in English) and when these things do something, they use a method (like verbs in English) and when describing these things you use properties (like adjectives in English). All code starts with Sub MACRO NAME () and ends with End Sub. When you need an object to do something or follow a method you type a period right in between the object and the method, object.method. When you need an object to have a specific characteristic or property you type a period in between the object and the property object.property. Lets quickly go over these elements in the below example. You will notice that the code for macro ht begins with Sub ht () then you have the description of the macro right below it, training managers monthly report (you can type a description when recording the macro for the first time), then you see in the next line, the keyboard shortcut, so in essence, the first couple of lines of code give you a quick summary of the macro.

6. In this example, when recording the original macro, I entered the names of the training managers. The first name was Mary Smith and this is why you see the reference ActiveCell.FormulaR1C1= Mary Smith. Visual Basic added this code after I entered the first name, then in the second line of code you see Range(A2). Select because range is a thing that is doing something or following a method, the method is that range A2 was selected and the name Mary Smith was entered. For the purposes of this tutorial, I will show how easy it can be to edit this type of code without really knowing much about code. We are going to copy and paste five times an existing piece of code with the instructions and edit accordingly. So, go ahead and copy the last two lines of code ActiveCell.FormulaR1C1= Mike Temples, Range(A6). Now paste it five times right below it. It should look like the image below.

7. Next, replace the name Mike Temple with the five new Training Managers, Marlene Evans, Jose Gonzalez, Melvin McCall, JR Fallon and Peter Johnson. Enter A7, A8, A9, A10 and A11 in the Range(A6).Select piece of code. It should look like the image below.

8. Now go to File>Save PERSONAL.XLS and save the macro changes within the personal macro workbook. Then go to File>Close and Return to Microsoft Excel to close the macro within Visual Basic.

9. Finally, we are going to test it, now that you are back in Excel, go to File>New to open a new spreadsheet. Press the control key and the h key on your keyboard to test the shortcut for the macro. The new names should appear along with the old ones. Please remember that you can also run the macro by going to Tools>Macro>Macros and selecting the macro and clicking the run button.

Congrats! you actually worked on some code and learned how to edit macros. Anything new like a computer program or language can be intimidating especially if you dont work with it on daily basis and dont have much time to play with it. And many times, we would much rather continue doing things the long way than find shortcuts, but when you take a little time to learn something that seemed hard, you usually find it is not so hard and can really help you get things done easily and quickly. Remember, this tutorial is an introduction to Visual Basic within Excel and does not cover all the elements, capabilities or functions of Visual Basic; but I will soon be posting more in depth tutorials related to macros that will help you understand the Microsoft Visual Basic application better.

1. 2. 3. 4. 5.

Start Microsoft Word. Create a new blank document. On the Tools menu, point to Macro, and then click Macros. In the Macros in box, click Normal.dot (global template). In the Macro name box, type ChangeColor, and then click Create. The following code appears in the Visual Basic Editor: Sub ChangeColor() ' ' ChangeColor Macro ' Macro created {date} by {name} ' End Sub

6. Next, edit the code so that it looks like this: Sub ChangeColor() ' ' ChangeColor Macro ' Macro created {date} by {name} ' Dim intRed As Integer Dim intGreen As Integer Dim intBlue As Integer intRed = InputBox("Red value? (0-255)") intGreen = InputBox("Green value? (0-255)") intBlue = InputBox("Blue value? (0-255)") ActiveWindow.View = wdWebView With ActiveDocument.Background.Fill .Visible = msoTrue .ForeColor.RGB = RGB(intRed, intGreen, intBlue) End With MsgBox "The document's background is now RGB(" & _ intRed & ", " & intGreen & ", " & intBlue & ")." End Sub 7. On the View menu, click Microsoft Word. 8. Type some text in the document so you can see the contrast between the document's background and the text. 9. On the Tools menu in Word, point to Macro, and then click Macros.

10. Click ChangeColor, and then click Run. Note If a message appears stating that the macros in the project are disabled, do one or both of the following: Refer to the Microsoft Product Support Services article WD2002: "The Macros in the Project Are Disabled" Message When You Run a Macro (Q290949) to enable the macro to run properly. Refer to the assistance article Changing Macro Security Settings in Office XP. 11. In the dialog box that appears, type a whole number between 0 and 255 three times (for each value of red, green, and blue), and click OK each time. The document's background color changes to match the combined red-green-blue (RGB) value you provided in the previous step. Now, let's use the Visual Basic Editor to modify the macro. Let's change the background color of all of the text in the Word document based on the color values you specify. To do this: 1. On the Tools menu, point to Macro, and then click Visual Basic Editor. 2. In the Project window, expand the Normal folder if it is not already expanded. Note If the Project Explorer window is not visible, on the View menu, click Project Explorer. 3. In the expanded Normal folder, expand the Modules folder if it is not already expanded. 4. Double-click NewMacros. 5. Locate the ChangeColor macro code from the previous procedure. Note If you have problems locating the ChangeColor macro, try clicking ChangeColor in the Procedure list at the top right edge of the Code window. 6. Next, edit the code so that it looks like this (changes from the previous code are highlighted in bold): Sub ChangeColor() ' ' ChangeColor Macro ' Macro created {date} by {name} ' Dim intRed As Integer Dim intGreen As Integer Dim intBlue As Integer intRed = InputBox("Red value? (0-255)") intGreen = InputBox("Green value? (0-255)") intBlue = InputBox("Blue value? (0-255)") ActiveWindow.View = wdWebView With ActiveDocument .Select .Range.Font.Color = RGB(intRed, intGreen, intBlue)

End With MsgBox "The document's text is now RGB(" & _ intRed & ", " & intGreen & ", " & intBlue & ")." End Sub 7. Click anywhere in the ChangeColor macro between the Sub and End Sub keywords. 8. On the Run menu, click Run Sub/UserForm. 9. In the dialog box that appears, type whole numbers between 0 and 255 for each value of red, green, and blue, and then click OK for each of the red, green, and blue input boxes that appear. The document's text color changes to match the combined red-green-blue (RGB) value you provided in the previous step.

You might also like