VBA Macros

Getting you started with VBA Macros

What is a VBA Macro

A macro is a Sub procedure that performs a specific task within a program, and

  • has no parameters, and
  • is located in a standard module.

(MS Access) Macro or VBA

In MS Access an alternative approach to programming was developed intended to replace VBA which is called 'Macros'. These 'Macros' can be used in Access Web Apps that are published on a Sharepoint Server which VBA can not. However for desktop solutions VBA is much more powerful and flexible than Macros: you can make API calls, read and write to text files, access other databases or data stores using ADO, run DAO loops through records, and write many other complex features.

Recording a macro

The macro recorder provided with Excel and Word allows you to record the actions on your Excel workbook or Word document interpreted as a sequence of VBA statements. As such using the macro recorder is the simplest way to create a macro that can reproduce exactly those actions. It can only interpret your actions as a sequence, it does not understand the other two structure elements of a program: selection and iteration (choice and repetition)-which are required in almost any program- so the use of the recorder code is limited.

Creating a macro in the VB Editor

Te enter the below code, you must first have a standard module open. If you have Code VBA add-in installed, such a module is automatically added if missing in your project. Without it, in case of Excel, you will likely have the the cursor in Sheet1 class module and your attempt to run the macro will fail.

Sub MyFirstProcedure()
    MsgBox "Hello World", vbOKOnly + vbInformation    
End Sub

Start code execution

Running this code (F5) displays a message box, as you would expect.

Also, you could have started it from the Application's ribbon » Developer tab » Visual Basic group » Macros dialog. Alternatively, with Code VBA installed, this dialog can also be started from menu: Code VBA » Procedure » View Macro

your first macro