官术网_书友最值得收藏!

Investigating VBA code

In this recipe, we will inspect a short VBA subroutine. Like all programming languages, VBA has a specific syntax, and the best way to understand the principles is to see what the coding looks like.

What we're going to see here will lay the foundation for the other recipes in this book, so pay close attention.

Getting ready

In order to investigate and edit VBA code in Excel, or any of the other Office applications, we first need to activate the Developer toolbar. Here are the steps:

  1. Open MS Excel and select Blank workbook from the opening screen.
  2. Open the 01_VBA_Code.xlsm sample file. Click on [Enable Content] on the SECURITY WARNING ribbon.
  3. If the Developer ribbon is not visible, activate the Backstage View by clicking on File, which will display the following screen:

    Figure 1.1 – The Backstage View

  4. From the category list on the left, select the last option, Options. The Excel Options dialog box appears:

    Figure 1.2 – The Excel Options dialog box

  5. From the categories on the left, select Customize Ribbon.
  6. To the far right, under the Main Tabs heading, look for the Developer option. Select the checkbox.
  7. Click on OK to accept the change. The dialog box will close, and Excel will now display the Developer tab.

How to do it…

With the Developer tab activated, we will now proceed with the steps for this recipe:

  1. With 01_VBA_Code.xlsm open, click on the Developer tab:

    Figure 1.3 – The Developer tab

  2. In the Code group (the first group on the left of the ribbon), select the Macros icon. The Macro dialog box opens:

    Figure 1.4 – The Macro dialog box

  3. The VBA code we want to investigate is contained in the only macro: Area_Bold. Click on the Edit button. The Microsoft Visual Basic for Applications window will open. Maximize the window, if necessary:

    Figure 1.5 – The Microsoft Visual Basic for Applications editor window

  4. In the code window (the large area on the right), a short VBA subroutine is displayed.

How it works…

The subroutine in the VBA Editor looks like this:

Figure 1.6 – The subroutine in the VBA Editor

Let's try to understand this subroutine. Any and all subroutines in VBA start with the Sub keyword, followed by the name of the subroutine (macro), and end with two brackets.

A single apostrophe allows you to enter a note. Comments can be added anywhere in a subroutine and will be displayed in green text, as long as it is on its own line or after a line of code (that is, it cannot be before a line of code as it would obviously comment out the code too). Indented lines without apostrophes are VBA instructions. In this case, the first instruction is when the range B1 to E1 is selected.

The selection's font style is then set to Bold. The last instruction moves the focus to cell A1. Finally, the subroutine is concluded with the End Sub keywords.

There's more…

Whether you record a macro or manually type the coding, you will always find this basic syntax structure in VBA.

In future chapters, we will be working with much longer subroutines. Before we get there, though, we are going to record a macro. With your newly acquired knowledge, you will be able to investigate the VBA code for that too.

主站蜘蛛池模板: 旬邑县| 商都县| 出国| 天镇县| 柳州市| 乌海市| 横峰县| 石河子市| 潮州市| 扎兰屯市| 鱼台县| 古交市| 永福县| 临沧市| 通辽市| 涿州市| 会同县| 甘谷县| 图片| 铜陵市| 威海市| 云梦县| 淄博市| 慈溪市| 龙岩市| 定远县| 澳门| 晋城| 大足县| 夏邑县| 绩溪县| 旅游| 咸宁市| 龙游县| 神木县| 三台县| 公安县| 河西区| 互助| 威远县| 宁南县|