Главная » Excel Macros For Dummies читать онлайн | страница 52

Читать книгу Excel Macros For Dummies онлайн

52 страница из 60

Ready to enter some real, live code? Try the following steps:

1 Create a new workbook in Excel.

2 Press Alt+F11 to open the VBE.

3 Click the new workbook’s name in the Project Explorer.

4 Choose Insert ⇒ Module to insert a VBA module into the project.

5 Type the following code into the module: Sub GuessName() Dim Msg as String Dim Ans As Long Msg = "Is your name " & Application.UserName & "?" Ans = MsgBox(Msg, vbYesNo) If Ans = vbNo Then MsgBox "Oh, never mind." If Ans = vbYes Then MsgBox "I must be clairvoyant!" End Sub

6 Make sure the cursor is located anywhere within the text you typed and press F5 to execute the procedure.

When you enter the code listed in Step 5, you might notice that the VBE makes some adjustments to the text you enter. For example, after you type the Sub statement, the VBE automatically inserts the End Sub statement. And if you omit the space before or after an equal sign, the VBE inserts the space for you. Also, the VBE changes the color and capitalization of some text. This is all perfectly normal. It’s just the VBE’s way of keeping things neat and readable.

Правообладателям