Introduction to Excel Macro
What is more interesting about a Excel VBA Macro over other Programming Languages?
Excel has a
- Very sophisticated database in the form of worksheets,
- Integrated with a VB editor that helps to build code in minutes and
- Execute the code without worrying about Compilation, Build or Linking to EXE etc.,
Let me introduce to this Alien who is familiar to lot of Programmers.
1. Writing a “Hello World” Program in VBA
“Hello World” in VBA is so easy like ‘A for Apple’. Code is Simple: Msgbox “Hello World”. That’s it.
‘Msgbox’ is a built-in function used to display a Message in a pop-up window. Course Completed.
But finding where to write this code and how to execute the code is the tricky part. Follow below steps.
2. Where to Write the Code: Find VB Editor
1. Open a new excel workbook.
2. Press Alt+F11. VB editor for Excel will appear. (To use this facility, Microsoft Office Excel should have installed with VBA option enabled)
3. On the left hand side panel, there will be options for Sheet1, Sheet2, Sheet3 & Thisworkbook.
- (a) Inside Thisworkbook – you can write Macro code common for the whole workbook.
- (b) Inside Sheet1 – You can write code specific to Sheet1.
- (c) Sheet2 & Sheet3 – same as step b.
Now, Double click “Thisworkbook”. That means, we will write a “Hello world” Program common for this workbook.
4. Click the Drop-down near text “General” just above the space left for coding.
5. Choose “Workbook”. A sub will get inserted with name “Workbook_Open()”.
a. Inside that sub or sub-routine, write the command: Msgbox “Hello World”. or Copy Paste below code.Visual Basic123Private Sub Workbook_Open()MsgBox "Hello World"End Sub
3. How to Execute the Code
Press F5 to test whether the code is correct. If it is correct a message box will appear displaying “Hello World”
Now the important step. Store all the precious work you have learned. Save the workbook with “Macro Enabled Workbook” (XLSM). Close the workbook and open it with macros enabled. The Hello world message box will appear every time the document is opened.
End of “Hello World” Excel VBA Macro Programing. Now you are familiar with this VBA Alien, what to do next – Get ready to Roam in Space. But be ready to equip with special tips in upcoming posts.
Next Post: Find Duplicates in Excel & Google Sheets Remove Duplicates
Excel VBA – For Beginners – Hello World Program