Wednesday, October 16, 2019

HelloWorld-Macro

Hello World Macro

This would easily be the easiest first macro for anybody to create, especially if one is just beginning to learn how to program macro's in the Visual Basic Editor in Microsoft Excel (Any version). The first step is to create a workbook and call it "Hello_World_Macro.xlsm" and save it to your computer in your favoured location, usually "My Documents".

STEP 1: Create a Shape or WordArt:

Refer to Figure 1 above, in this case I have used a WordArt Text on the worksheet from where I want to execute the macro.

STEP 2: Add the following code:

Sub Hello_World()
'---------------------------------------------------------------------------------------
' Procedure : Hello_World
' Author    : vsmathur-onms
' Date      : 17/10/2019
' Purpose   :
'---------------------------------------------------------------------------------------
'

   On Error GoTo Hello_World_Error

    MsgBox "Hello World !!", vbOKOnly

Hello_World_Exit:

    On Error GoTo 0
    Exit Sub

Hello_World_Error:

    MsgBox "Error " & Err.Number & " on Line # " & Erl & " (" & Err.Description & ") in procedure Hello_World of Module m_HelloWorld"
    Resume Hello_World_Exit


End Sub

STEP 3: Assign Macro and Click to Execute Macro

You can right-click the WordArt created in Step 1 above and select "Assign Macro" - in the dialog box that opens, select the Sub called "Hello_World". Finally, click the WordArt and you should see a Message Box appear with "Hello World !!" written in it. There, that is all that there is to creating your very first Macro!! Cheers !!

CA Vikram S. Mathur17-Oct-2019 | 10:15 Hours IST