Sponsored Link

Sponsored Link

Friday, September 12, 2008

VBA, Simple Macro Example

The purpose of creating this page is to help you understand how to declare a integer variable in VBA, assign value to it and compare it using IF THEN and show the result using using message box. However, in near future we will move step by step to more complex conditions.

1) Dim xValue As Integer -> Creates the integer variable which can hold numeric values and in case if you are not sure about the user input, please use variant type.
2) xValue = Sheet1.Cells(3, 4) -> This part of code is written under 'Private Sub CommandButton1_Click()' hence will only execute when someone clicks on the command button 1 which is placed on sheet1.
3) If xValue >= 18 Then MsgBox "You are major", vbInformation + vbOKOnly, "IF Then Macros"Else MsgBox "You are Minor", vbInformation + vbOKOnly, "IF Then Macros"End If
The above code check if the xValue is greater or equal to 18 and display the necessary output using the msgbox.
The link below has MS Excel workbook with above macro for download.

MS Excel Simple macro download

Note: To view the code, press ALF +F11.

Also, do let me know your comments about this post and subscribe to us to receive latest updates via email

We assure you knowledge, not SPAM!


vinu said...

Hello sarfaraz..

If you could start up VBA by posting lessons from scratch level with more practical way..It would be helpful for all..you can post in your orkut community also.


Post a Comment