Excel VBA makes working with excel easier. We can automate the tasks in excel by writing what we call macros. I would start with the basics first, we will 1 st create a simple macro and before then turning the developers tab on Developer Tab To turn on the Developter tab, execute the following steps. 1. Right click anywhere on the ribbon, and then click Customize the Ribbon. 2. Under Customize the Ribbon, on the right side of the dialog box, select Main tabs and check the Developer check box.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Excel VBA makes working with excel easier. We can automate the tasks in excel by writing what we call macros. I
would start with the basics first, we will 1st create a simple macro and before then turning the developers tab on
Developer TabTo turn on the Developter tab, execute the following steps.
1. Right click anywhere on the ribbon, and then click Customize the Ribbon.
2. Under Customize the Ribbon, on the right side of the dialog box, select Main tabs and check the Developer check
box.
3. Click OK.
4. Now Developer tab should be visible to you next to the View tab.
Let us make a simple command button that will place a value in the cell.
Command ButtonFor inserting a command button execute the following steps.
1. On the Developer tab, click Insert.
2. In the ActiveX Controls group, click Command Button.
3. Drag a command button anywhere on your worksheet.
Assign a MacroAssigning means the code we will write later, we will assign that macro to the command button, execute the following
steps.
1. Select the Design mode.
2. Right click on Command Button( Name : Command Button1)
3. Click View Code.
The Visual Basic Editor appears.
For convenience open up the project explorer (window with the names Sheet1, Sheet2 and Sheet3). If the Project
Explorer is not visible, click View->Project Explorer. To add any code, open code window for that sheet, say for sheet
1 click Sheet1 (Sheet1).
3. For now write the code shown below between Private Sub CommandButton1_Click() and End Sub.
5. Close the Visual Basic Editor.
6. After you deselect the Design mode click on the command button on the sheet Result:
Bravo you've just created a macro in Excel!
Note: You might have question on what is Range. These will be covered in the later posts. Or you can take a look at
the Range post.
Visual Basic EditorThere is another way to open visual basic (apart from right clicking the command button and clicking View Code).
To open the Visual Basic Editor, on the Developer tab, you have to just click the Visual Basic button shown below.
The Visual Basic Editor appears.
Did you find this information/Blog helpful? If so please share it.
MsgBox The MsgBox is a dialog box in Excel VBA which you can use as to build pop ups or alerts or many other purposes. Place a command button on your worksheet and add the following code lines in the subroutine():
1. Let us start with a simple message.
MsgBox "this is Anurag"
The output will be, when you click the command button on the sheet this:
2. A little more advanced message. First, enter a number into cell A1 say 12.
MsgBox "Entered value is " & Range("A1").Value
Result when you click the command button on the sheet:
Note: we use the & operator to concatenate (join) two strings. Although Range("A1").value is not a string, it works
here.
3. To start a new line in a message, use vbNewLine.
MsgBox "This is fun" & vbNewLine & "working with macros"
Result when you click the command button on the sheet:
Did you find this information/Blog helpful? If so please share it.
Object HierarchyAs they say an object can be a variable, function, or data structure. In the object-oriented programming paradigm,
"object" refers to a particular instance of a class where the object can be a combination of variables, functions, and
data structures.
An object can contain another object, and that object can contain another object, etc. In other words, Excel VBA
programming involves working with an object hierarchy. If you are confused this post will take you out of it. Just Keep
in mind what is object, that’s it.
The mother of all objects is Excel itself. We call it the Application object or what we can say is at the top level we
have Application object and the application object contains other objects. It will easier to understand the concept with
an example.
For Example, The excel file contain many excel sheets sheet1, sheet2 etc, now technically speaking the Workbook
object is the Excel file. The Workbook object contains other objects, such as the Worksheet object. (sheet1, sheet2).
And the Worksheet object contains other objects, such as the Range object.
In the “Create a Macro” tutorial we learned how to run code by clicking on a command button and there we used the
following code line:
Range("A1").Value = "Hello"
But what we meant was in layman language Excel->Sheet1->Range->value