VBA HELLO WORLD PROGRAM

Table of Contents

INTRODUCTION

In the previous article, we learned about


how to open VBA, what are the different windows in VBA

how to save the file with VBA i.e. with Macro

what are the functions and procedures, how to add modules  and

what are the different terms which we are going to use in future articles?


Whenever we learn anything new, we are always keen to see it being executed. So in this article, we’ll execute our first VBA program.

Now onwards different functionalities will be discussed as we proceed further in the VBA course.

FIRST STEP

OPENING THE EXCEL

  • Double Click the Excel icon and start the application.
  • The Excel will ask you about the kind of worksheet we want to open.
  • Choose BLANK WORKBOOK and click ok.
  • Now we are inside the excel with a blank workbook opened before us.
EXCEL OPENING WINDOW

CHECKING THE STATUS OF DEVELOPER TAB

  • After opening the Excel
  • Check if DEVELOPER TAB is visible or not. If not refer to the previous post regarding the same process.
  • After the developer tab is visible, go to developer tab.
  • Click Visual Basic.
  • Now we have reached the visual basic window.
VBA WELCOME SCREEN

In the welcome window of Excel, you can see that I have already saved the file with a name.

That is all your preference as I just love to repeatedly do CTRL+S to keep the data saved to avoid any problem.

You can also save now or later by pressing CTRL+S or going to FILE MENU and then save. The save dialog box will open for the first time asking you for the location and name of the file and its type of course.

WRITING THE PROGRAM

OUT OF THE FOUR TYPES DISCUSSED, WE’LL USE THE SUBPROCEDURE FIRST.

Just to remind of the format of subprocedure 

SUB Name_of_subprocedure()

..instructions

…statements…..

end sub

so let us write our first program

HELLO WORLD-1-CODE

CODE

Sub HelloWorld()
MsgBox ("Hello World! This is my first program")

End Sub

ANIMATED PICTURE [HOW TO ENTER CODE]

VBA: ENTERING THE FIRST CODE

EXPLANATION

Maybe, some of you say, is that it?

The answer is YES.

That is it.

It is your first program. 

As you can see out of the three lines, the first and last are the standards which we have seen twice in the format of a subprocedure.

So the only line we entered, which is an instruction and the code which will do something is the middle one.

After this, click the save button to save the procedure.


  MsgBox(“Any Text”)

This method displays the text entered in a small window which you must have seen in many applications.

RUNNING OUR FIRST PROGRAM

As soon as we make a sub procedure, it directly goes into the MACRO LIST of EXCEL with the name of the subprocedure and we can find it there.

We can run our code in two ways

1. Through the VBE only.

2. Through MACRO. [which is our ultimate aim as VBA code will be used with Excel]

Let us try both for the first time.

HOW TO PUT COMMENTS IN VBA?

We can easily insert comments in VBA by simply starting it with an ‘ [apostrophe]
Simply add an apostrophe [ ‘ ] and everything written after this is a comment which won’t be read as the statement.

RUNNING PROGRAM THROUGH VBE

After saving the program.

  • Go to the RUN MENU and click RUN SUB…
  • or Click the RUN BUTTON from the TOOLBAR
  • The MACRO window will open to select the macro to run.
  • Select the MACRO HelloWorld and click RUN.

The code will run and the output will be shown.

The complete process is animated below.

VBA: RUNNING THE FIRST CODE THROUGH VBE

RUNNING PROGRAM THROUGH MACRO

When we have checked the program in VBE, finally we have to execute it through the EXCEL ONLY.

So it’s necessary that we should know how we’d be using the procedures written in VBA through Excel.

  • Go to VIEW TAB
  • At the right side, the last button is MACRO.
  • Click MACRO and the MACRO dialog box, similar to the one we saw in VBE will open.
  • Choose the MACRO to be executed and click RUN.

The program will run.

VBA: RUNNING THE FIRST CODE THROUGH EXCEL

CONGRATULATIONS!!! FIRST PROGRAM

HELLO WORLD-2-CODE

CODE [HELLO WORLD IN SHEET]

Sub HelloWorldInSheet()
Range("A10").Value = "HELLO WORLD! THIS IS MY FIRST PROGRAM"
End Sub

PICTURE [HOW TO ENTER CODE]

VBA: ENTERING THE CODE EXAMPLE 2

EXPLANATION

We can write many subprocedures on the same page as you can see in the picture above.

Write the code given and click Save.

The code contains the main line as

Range(“A10”).Value = “HELLO WORLD! THIS IS MY FIRST PROGRAM”

This function pics up cell A10 and change its VALUE to the text given in the “”.

RANGE(“CELL ADDRESS”) & RANGE(“START_ADDRESS, END_ADDRESS”)

This method refers to the address of the cell address given.
The same method in another format can also be used to refer to a range

RUNNING THE PROGRAM

As soon as we make a sub procedure, it directly goes into the MACRO LIST of EXCEL with the name of the subprocedure and we can find it there.

We can run our code in two ways

1. Through the VBE only.

2. Through MACRO. [which is our ultimate aim as VBA code will be used with Excel]

Let us try with both for the first time.

RUNNING PROGRAM THROUGH VBE

After saving the program.

  • Go to the RUN MENU and click RUN SUBPROCEDURE.
  • or Click the RUN BUTTON from the TOOLBAR
  • The MACRO window will open to select the macro to run.
  • Select the MACRO HelloWorld and click RUN.

The code will run and the output will be shown.

The output window is shown below.

VBA: RUNNING THE FIRST CODE THROUGH VBE

RUNNING PROGRAM THROUGH MACRO

When we have checked the program in VBE, finally we have to execute it through the EXCEL ONLY.

So it’s necessary that we should know how we’d be using the procedures written in VBA through Excel.

  • Go to VIEW TAB
  • At the right side, the last button is MACRO.
  • Click MACRO and the MACRO dialog box, similar to the one we saw in VBE will open.
  • Choose the MACRO to be executed and click RUN.

The program will run.

VBA: RUNNING THE FIRST CODE THROUGH EXCEL