Understand the Basic Concepts of VBA Syntax in Microsoft Office.

The Concept of Constants and When to Use Them

A constant is a value that is fixed and does not change during the program. By using constants, the code becomes easier to read and manage, as you can name frequently used values. To declare a constant, use the keyword Const. An example of a constant declaration is:

Const PI As Double = 3.14159

You should use constants when they have values that won’t change during the program, such as mathematical values (for example, PI), certain constraints, or fixed text. This way, if there is a change in value in the future, you just need to change it in one place.

3. Statements and Operators

In VBA, statements are lines of code that describe an action, define an item, or assign a value to a variable. Statements function like sentences in everyday language.

Normally, each line of code has only one statement, but you can put multiple statements in a single line by separating them with colons. Examples of statements include declaring variables, calling procedures, or setting the value of an object’s property.

A statement can be in the form of:

  1. Variable declaration: For example, Dim x As Integer.
  2. Grade assignment: For example, x = 10.
  3. Procedure or function calls: For example, Call MsgBox(“Hello!”).
  4. Flow control: For example, If, For, or Do While statements.

Arithmetic Operator

Arithmetic operators are used to perform mathematical calculations.

OperatorDescriptionExampleResult
+Addition5 + 38
Reduction10 – 46
*Multiplication6 * 742
/Division20 / 45
^Rank2^38
ModThe rest of the division10 Mod 31

Logic Operator

Logical operators are used to make decisions based on condition statements.

OperatorDescriptionExampleResult
AndWorth True if both conditions are worth True(5 > 3) And (2 < 4)True
OrWorth True jika salah satu kondisi True(5 > 3) Or (2 > 4)True
NotInverting condition valuesNot (5 > 3)False

4. Writing and Running Macros

To create a macro in VBA, you need to write a procedure that is constrained by Sub and End Sub statements.

Once you’ve created a macro, you can run it through the Macros Dialog Box. Here are the steps to run the macro:

  1. Open a Microsoft Office application (such as Excel or Word).
  2. Click the Developer tab in the Ribbon. If this tab doesn’t appear, you may need to enable it in settings.
  3. Click the Macros button to open the dialog box.
  4. In the dialog box, select the name of the macro you want to run from the list.
  5. Click the Run button to execute the selected macro.

5. Manipulation of Objects and Properties

In VBA, an object is an element that can be changed through code. Two objects that are often used in Microsoft Word are ActiveDocument and Selection.

  • ActiveDocument: This is an object that represents the document that is currently open in Word. You can access and change the properties and methods of this document.
  • Selection: This is an object that represents the text or element that is being selected in the document. If no one is selected, this object will indicate the cursor position.

You can change various object properties in VBA to format the document. Some of the properties that are frequently changed are:

  • Font: Change the text font type.
  • Size: Resize the text.
  • Bold: Determines whether the text is displayed in bold.

Latest Articles