×
英语
很抱歉。本文没有你的语言版本。

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.

扩展你的 Office 技能
了解培训
抢先获得新功能
加入 Office 预览体验计划

此信息是否有帮助?

谢谢您的反馈!

谢谢你的反馈! 可能需要转接到 Office 支持专员。

×