When you are saving the workbook, we need to choose the file type from the list of file type options as below.īy default, we can use the “ Excel macro-enabled workbook”. We have two different ways to save our macro files. Saving macro workbook is also same but one small change will be there. Step 7: Now, if we click on the shape the macro will run and give the result as below. Select the macro name and click on the Ok button. Step 6: A pop-up box will come asking for macro selection. Step 5: Now, we need to assign the macro to that shape, for that select the shape and right click and choose “ Assign Macro” option. For adding the text just right click on a shape and select Edit Text Step 3: Now, we can write a text like “click here” or “run Macro” in that shape. Step 2: After selecting the shape, draw this on your worksheet. Step 1: Go to Insert, and Select the shape as per your wish. If we want to run the macro from the worksheet itself with the help of a shape, we can do that. Follow the below steps to assign the macro to shapes. Step 5: Go to excel and check whether the results are appearing or not as below.Įxample #2 – Attaching a Macro to a Shape If multiple macros are available need to choose the correct macro and click on the run button.
Step 4: Once we click on run, a pop up will ask for a run as below.
#HOW TO WRITE A MACRO IN EXCEL 210 CODE#
Step 3: We can run the code by pressing F5 or click on the run button at the top which is marked in Black colored box. Cells(1,1).value represents the cell of row1 and column1 value. In the above macro, I wrote a sample code like when I run the macro first three cells should fill with Name, Age and Address. Step 2: In between, we should write our code. When we enter after the brackets automatically “End Sub” will appear. Step 1: Any program in VBA starts with sub keyword and program name with open and close brackets as below. When we insert a new module, the empty window will look like below.įollow the below steps to write code in excel VBA. Now we will see a sample macro code using a normal module.
#HOW TO WRITE A MACRO IN EXCEL 210 DOWNLOAD#
You can download this VBA Code Excel Template here – VBA Code Excel Template Example #1 – Macro Code using a Normal Module
Examples to Write and Run Code in Excel VBAīelow are the different examples to write and run the code using VBA Code in Excel. Depend on the type of requirement we should choose the module and write the code in it.
These are the modules where we can write our code. Similarly, we can insert a class module and user form also. We need to insert the module by clicking on the Insert menu and select the module option to insert a module. Module: Normally we will write code in modules in VBA. Suppose if we write an event for “New sheet” then whenever we created a new sheet the macro will run for that workbook. Suppose if we write an event code for the workbook then whenever that event happened in any of the sheets of the workbook the macro will run. Whatever the code written in this module will apply for all the sheets in the workbook. Whenever that event happened the macro will run. VBA comes with multiple events shown in the above screenshot, events like change, deactivate and many more. You may be in confusion what is an event code. Sheet Module Code: Whatever the event code we write in the sheet module it will apply for that sheet alone. If we add multiple sheets in our workbook it will allow different sheet modules here. By default, there will be two modules available to write your code, one is sheet1 module and workbook module.