PROVIDING ACCURATE, RELIABLE AND STRAIGHT TO THE POINT KNOWLEDGE ABOUT DIFFERENT IT TOOLS EXPLAINED WITH PRACTICAL EXAMPLES

CONTENTS

VBA HELLO WORLD PROGRAM

INTRODUCTION

In the previous article we learnt 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 the 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
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 FIRST SCREEN
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 location and name of the file and its type ofcourse.

WRITING THE PROGRAM

OUT OF THE FOUR TYPES DISCUSSED, WE’LL USE THE SUB PROCEDURE FIRST.
Just to remind of the format of sub procedure
 
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]

ENTERING THE FIRST VBA CODE
VBA: ENTERING THE FIRST CODE

EXPLANATION

May be , some of you say, is that it? The answer is YES. That is it.
Its 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 sub procedure. 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.
 

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 sub procedure 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 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 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 its 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.

gyankosh RUNNING VBA PROGRAM THROUGH MACRO
VBA: RUNNING THE FIRST CODE THROUGH EXCEL
CONGRATULATIONS!!! YOU HAVE CREATED, YOUR 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]

GYANKOSH HELLO WORLD VBA EXAMPLE 2
VBA: ENTERING THE CODE EXAMPLE 2

EXPLANATION

We can write many subprocedures in the same page as you can see 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 the cell A10 and change its VALUE to the text given in the “”.

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 sub procedure 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 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 output will be shown.

The output window is shown below.

RUNNING CODE THROUGH VBE EXAMPLE 2
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 its 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

OTHER WAYS TO REACH THIS ARTICLE

  • WHAT IS EXCEL, HOW EXCEL WORKS, WRITE FORMULA IN EXCEL, EXAMPLE OF USING FORMULA IN EXCEL, ANIMATED EXAMLPE OF USING FORMULA IN EXCEL

LEARN

Encourage us by following at …

If you have any queries or suggestions or any problem which you would like us to solve for you, kindly mail at

gyankosh060309@gmail.com

Pin It on Pinterest

Share This

KINDLY SPEND FEW SECONDS HERE....

%d bloggers like this: