Saturday, November 24, 2018

Step 2 - Read and understand the code

Step 2 - Read and understand the code

In Step 1, you recorded your first VBA macro to delete a row. Let's now look at the code generated by the macro recorder.

To view the code, click Tools > Macro > Visual Basic Editor. Here's what you'll see:

  Sub DeleteRow()  '  ' DeleteRow Macro  '  '   Selection.Rows.Delete     End Sub    

Let's review each line in this macro:

  1. Sub DeleteRow

    All macros begin with Sub and end with the End Sub keywords. In the above example, DeleteRowis the name of the macro or procedure that you created.

  2. ' DeleteRow Macro

    This is called comments. Comments are useful to describe what's going on in your code. This is especially helpful when your code becomes more complex and other people need to maintain your code, or even when you revisit your code after sometime. Comments always start with a single quote, and the font color becomes green in the Visual Basic Editor to help you differentiate comments from other parts of your code.

  3. Selection.Rows.Delete

    This line of code tells Word what to do. In this case, select the row and delete it.

Now that you have created your first macro and reviewed the basic parts in a macro, let's move to the next section where you'll learn a few programming fundamentals.

No comments:

Post a Comment