This is a blog that discusses anything related to Visual Basic and provides updates for VISUAL BASIC TUTORIAL website. Your comments and suggestions are welcome here. Thank you.
Tuesday, May 19, 2009
Excel VBA Objects
An Excel VBA object has properties and methods. Properties are like the characteristics or attributes of an object. For example, Range is an Excel VBA object and one of its properties is value. We connect an object to its property by a period(a dot or full stop). The following example shows how we connect the property value to the Range object.
Example 8.1
Private Sub CommandButton1_Click()
Range("A1:A6").Value = 10
End Sub
In this example, by using the value property, we can fill cells A1 to A6 with the value of 10. However, because value is the default property, it can be omitted. So the above procedure can be rewritten as
Example 8.2
Private Sub CommandButton1_Click()
Range("A1:A6")= 10
End Sub
To learn more about Excel VBA, log on to VBA tutorial site:
http://www.vbtutor.net/VBA/vba_tutorial.html
Saturday, May 9, 2009
Excel VBA
VBA is the acronym for Visual Basic for Applications. It is an integration of the Microsoft's event-driven programming language Visual Basic with Microsoft Office applications such as Microsoft Excel, Microsoft Word, Microsoft PowerPoint and more. By running Visual Basic IDE within the Microsoft Office applications, we can build customized solutions and programs to enhance the capabilities of those applications.
Among the Visual Basic for applications, Microsoft Excel VBA is the most popular. There are many reasons why we should learn VBA for Microsoft Excel, among them is you can learn the fundamentals of Visual Basic programming within the MS Excel environment, without having to purchase a copy of Microsoft Visual Basic software. Another reason is by learning Excel VBA; you can build custom made functions to complement the built-in formulae and functions of Microsoft Excel. Although MS Excel has a lot of built-in formulae and functions, it is still not enough for certain complex calculations and applications. For example, it is very hard to calculate monthly payment for a loan taken using Excel's built-in formula, but it is relatively easy to program a VBA for such calculation. This book is written in such a way that you can learn VBA for MS Excel in an easy manner, and everyone shall master it in a short time!
You can program Excel VBA in every version of Microsoft Office, including MS Office 97, MS Office2000, MS Office2002, MS Office2003 and MS Office XP. The reason VBA is needed is due to the limitations in using the built-in functions of MS Excel and macro recording. By using VBA, you can build some very powerful tools in MS Excel, including financial and scientific applications such as getting financial data from the Internet as well as linear programming.
You can learn about Excel VBA in my VBA tutorial at:
http://www.vbtutor.net/VBA/vba_tutorial.html
Tuesday, May 5, 2009
Prime Number Tester
In this program, I use the Select Case ......End Select statement to determine whether a number entered by a user is a prime number or not. For case 1, all numbers that are less than 2 are prime. In Case 2, if the number is 2, it is a prime number. In the last case, if the number N is more than 2, I need to divide this number by all the numbers from 3,4,5,6,........up to N-1, if it can be divided by any of these numbers, it is not a prime number, otherwise it is a prime number. I use a Do......Loop While statement to control the program flow. Here I also used a tag="Not Prime' to identify the number that is not prime, so that when the routine exits the loop, the label will display the correct answer.
Please refer to the link below for the source code.
http://www.vbtutor.net/VB_Sample/Prime.htm
Thursday, April 23, 2009
Visual Basic Polling System
Survey and polling tools are often used in marketing or politics to assess
rating for certain services or products. Polling tools can take many forms, some just use a simple dichotomous scale of Yes and No, or Likert Scale that consists of three or more choices. You can create Polling tool in Visual Basic easily by using the option buttons. In my program, I give the users five choices, Excellent, Very Good, Good, Satisfactory and Bad. Then the results are presented in frequency and percentage respectively.
To learn how to program the above polling system in VB, please view my sample polling program at
Sunday, April 12, 2009
VB Snakes and Ladders game
To design the program in VB, it took me one day to think out the mathematical logics and wrote out the routines on paper (not in front of the computer). Since the board comprises 10 rows and 10 columns, each box thus represents a cell with the coordinate (column, row). Therefore, I figured out that I need to define the coordinate of every cell by declaring two arrays row(10) and col(10) at the beginning of the procedure. To move the chess piece, I employed the method object.move col(i), row(j), where I initiate the values of col(i) and row(j) in a For....Next loop. As the motion is in a zigzag manner, I need to control the motion using reverse order and by imposing some conditions.
To learn more about the program, check out the link below:
Snakes and Ladders Game
Saturday, April 4, 2009
Microsoft Multimedia Control
To be able to play multimedia files or multimedia devices, you have to insert Microsoft Multimedia Control into your VB applications that you are going to create. However, Microsoft Multimedia Control is not normally included in the default toolbox, therefore you need to add the MM control by pressing Ctrl+T and select it from the components dialog box that is displayed.
To learn how to create multimedia applications, please refer to the following lessons in Vbtutor.net.
http://www.vbtutor.net/lesson19.html
http://www.vbtutor.net/lesson20.html
http://www.vbtutor.net/lesson21.html
http://www.vbtutor.net/lesson22.html
Sunday, March 29, 2009
The use of Option Exlicit in VB program
The use of Option Explicit is to help us to track errors in the usage of variable names within a VB program code. For example, if we commit a typo, Visual Basic will pop up an error message “variable not defined”. Indeed, Option Explicit forces the programmer to declare all the variables using the Dim keyword. It is a good practice to use Option Explicit because it will prevent us from using incorrect variable names due to typing errors, especially when the program gets longer. With the usage of Option Explicit, it will save our time in debugging our programs. For example, In the following program, a typo was committed, EmployeID, which was declared as EmployeeID.
Option Explicit
Private Sub Command1_Click()
Dim EmployeeID As String
EmployeID = "John"
Text1.Text = EmployeeID
End Sub
When you run the program, a dialog box with an error message 'Variable not defined" will appear ,when you click the OK button, it will show the highlighted typo, enabling us to correct the mistake.