Monday, September 21, 2015

Development Environment

See the developer tab which we added, then click the Visual Basic button. It enables us to open up the VBA Editor in Excel. Note that there are a few other ways to open it up. We use this way anyway for a while.
the image: cited from HomeLearn

Seeing the display, we notice that there is an area on the left named as Project - VBA Project. The white area show all the object that your project currently has (you don't need to what the object is).  By default, there are three Sheet objects: Sheet1, Sheet2, and Sheet3 which correspond with the Excel worksheets.

The fourth thing called ThisWorkBook refers to the workbook where all your current macros are.
Note that, if we record a macro, Personal Macro Workbook appears (which is not visible above yet). We can use Personal Macro Workbook to store macros that you use quite frequently.

There is nothing on the grey area because we don't have any any coding window yet. Double click the sheet1 on left, then we will see that a window appears, which is the coding window. We will writes all codes on this coding window,

the image: cited from HomeLearn

Because we double-clicked the sheet1 and opened the coding window, the codes written on this coding window affect only "sheet1" worksheet. likewise, if we want to add codes which affect sheet2, we must double-click the sheet2 and open the coding window.

As a practice, we will write a program. We don't need to what the code means now. Add the following in the white area for Sheet1:
Sub HelloWorld() 
MsgBox "Hello VBA World!" 
End Sub
This simple program displays "Hello World".
the image: cited from HomeLearn

There is a blue word "Sub" at first line of the program, which corresponds with "End Sub". Sub is short for Subroutine, which is just a chunk of code that does a particular job. End Sub represents an end of the code chunk. The code chunk, Sub, needs a name followed by a pair of round brackets. Note that we can have anything you like as a name for your Sub. Though other coders wouldn't like it, you can even name it "donuts" or "banana."

In between "Sub" and "End Sub", we write actual codes for the Sub.

Once we write a code, we can run the code. Have a look at the tool bar above, then we will find a run button which seems like a play button of a video player.
Click it and we can run the code. Another way to run your code is to click the Run menu. From the Run menu, select Run Sub/User Form.

The MsgBox "" on the code stands for Message Box. We can display things which is in between double quotes.

The code we have added is supposed to display this message,


References

Home Learn "Excel VBA programming" accessed on 21st September 2015.