Thursday 18 December 2014

VBA PROGRAMMING

Programming on its own is an important aspect of computing, it enables us to turn the computer into a command-obeying machine. Through programming, we can control the computer to our taste; we can tell it what to do; when to do it; how to do it; in fact, we can tell the computer how to think! Surprised? Well you will soon find out how fun it is to become a programmer or a scripter.
For the purpose of this tutorial, we shall look into the in-built programming language of many applications, Virtual Basic for Application (VBA) by name. 
Visual Basic for Applications (VBA) is an implementation of Microsoft's event-driven programming language Visual Basic 6 and its associated integrated development environment (IDE).


VBA came into existence when the need for scripting is at climax. It is a general application language that commonly used applications like Microsoft Office and CorelDraw Graphic Suites make use of nowadays. With VBA, you can change the fonts, designs, or formats of your data. It is widely use in MS Word, PowerPoint, Publisher and Excel.

You can also read: Powerpoint VBA for Quiz Management
 
Code 1 (Our First Program)
VBA like other programming languages have two significant words that mark the beginning and the ending of its programs.
Below is a traditional ‘Hello World’ program in VBA.
Sub Helloworld()
Msgbox (“Helloworld!”)
End Sub.

Explanation
Sub Helloworld()  { Sub tells the computer that we want it to name our new program “Helloworld”
The “msgbox” made the computer to understand that we need a message box to display our message
Helloworld!” tells the computer to print what was in the colon on the message box screen (in this case ‘Helloworld’)
Then “End Sub” ends the program.
You can try changing the Helloworld into anything you wish and see the show flow!
·         You can load VBA via MS Word, PowerPoint or Excel. Open it via the Developer tab on the menu bar.

The InputBox
On many occasions, we may want the computer to collect some pieces of information (data like name, age or sex), in such case, we want the computer to provide a box with which we can input our particulars. That’s were inputbox come in.
Think of your computer as a big house containing several rooms with each room having different boxes. Let’s assume in one of the boxes, you want to keep names, in another you want to keep age. This means that the major aim of the inputbox is to keep records. You got that? Good! You’re great!
On this note, we are going to introduce an important aspect of VBA programming
Variables
Now, remember the previous illustration of a house with rooms of different boxes. Now, we want to store names but we need to tell the computer to name the box something like “Username”. That is where variables comes in, it comprises of anything ranging from numbers to letters or characters. Note that there are some words we can’t use because they were RESERVED WORDS.
Oh! We want the computer to save some data but in the computer’s memory (the illustration earlier), there were many rooms, some for letters, others for characters or numbers. This mean we will have to tell the computer the type of data we have planned to save. Will it be letters or numbers? That introduces us to the ‘dim’ function of the VBA.
‘dim’ is used as preface to tell the computer what type of data that will be stored.
Data types may be String, Integer, long, short, to name but few.

Code 2
We are going to write codes that prompt the user to input something, which will then be stored in the computer memory for later use.
Sub Username ()
Dim Username As String
Username = inputbox (prompt:=”Enter your name”, title:=”Name”)
End sub.
The second line above tells the computer to label a box ‘Username’; the box will contain string types of data.
The third line tells the computer to store anything typed in the input box into our previous ‘Username’ box.

I hope you had found this article useful. More updates soon. Drop your comments below.

No comments:

Post a Comment

Please drop your comments

Back to Top

Enter your email address:

Delivered by FeedBurner

Advertisement