How to write your first program in excel vba




VBA is a soft skill which is required in almost all industries irrespective of role. This tutorial is for beginners who just started to learn excel and vba.


This post illustrates how to write your first macro in excel.


1. Open Excel and hit Alt+F11 to open vba editor. By default you'll see 3 Sheet objects and 1 ThisWorkbook object. You can right click on project explorer window and insert Modules, Userforms and Class Modules.

 










VBA is event driven programming language. By event driven, I mean that there are set of events identified on which we can trigger a piece of code.For example, if i ask you to write "Hello" in A1 when you change value in cell B5. This is a cell value change event. Likewise we have lot of events. Few of them are at sheet level while rest are at workbook level.

Sheet Object   - We write code for sheet level events here. These events include:
SelectionChange
Change
Activate
BeforeDoubleClick
BeforeRightClick
Calculate
Deactivate etc.


ThisWorkbook Object  - We write code for workbook level events here. These events include:
BeforeClass
BeforeSave
NewSheet
SheetActivate
SheetChange
SheetSelectionChange etc.

Modules - Other functions, sub procedures that are generic and independent irrespective of sheet or workbook in which those exist, we write them in modules.
 
 Userforms - Form designing come into picture when we talk about userforms. Here we create custom forms equipped with items we require like listbox, textbox, labels etc.





2. After understanding the structure of vba project, it is right time to write a simple Hello World program. Insert a module and paste this code there.


          Sub Test()
                MsgBox ("Hello World!!!")
          End Sub

 

      

     
  





Great. You're now ready to watch more detailed tutorials.





Extreme Excel Solutions
http://www.ExtremeExcelSolutions.weebly.com