The Sub-procedure ends when Excel sees the phrase “End Sub” Which in computer lingo means, a group of related instructions meant to be followed together to do something meaningful. The word SUB indicates that the following lines of VBA are a sub-procedure (or sub-routine). Sub MakeMeRed(): This line tells Excel that we are writing a new set of instructions.Lets go thru the entire Macro code one line at a time. Well, what can I say, Excel is rather verbose when it is recording. If you have followed the instructions above, your code should look like this: You can see the VBA Code behind MakeMeRed.Double click on it to open it in Editor Area (top right, big white rectangle).Select Module 1 from left side area of VBE (called as Project Explorer).Understanding the Visual Basic Editor:īefore understanding the MakeMeRed macro, we need to be familiar with VBE (Visual Basic Editor). This opens Visual Basic Editor – a place where you can view & edit various VBA instructions (macros, code) to get things done in Excel. (You can also press ALT+F11 to do the same). To do this, right click on your current sheet name (bottom left) and click on View code option. Now that your first macro is working, lets peek behind the scenes and understand what VBA instructions are required to fill a cell with red. Go ahead and play with this little macro of ours. Whenever you click it, Excel would drop a bucket of red paint in the selected cell(s). Now, we have linked the rectangle shape to your macro. Step 6: Go ahead and play with your first macro And select the MakeMeRed macro from the list shown. Then right click on the rectangle shape and go to Assign Macro. Then, put some text like “click me to fill red” in it. Now go to Insert ribbon and draw a nice rectangle. Go to Developer ribbon and hit “stop recording” button. Now that you have done the only step in our macro, its time to stop Excel’s tape recorder. Just go to Home ribbon and fill red color in the current cell. Step 3: Fill the current cell with red color Just make sure there are no spaces or special characters in the name (except underscore) Just select any cell and go to Developer Ribbon & click on Record Macro button. Step 1: Select any cell & start macro recorder Make sure “Developer tab” is checked in right side area Check “Show Developer Tab in Ribbon” (3rd Check box)Ĥ. If you do not see Developer ribbon, follow these instructions.Ĥ. This is how our macro is going to work when it is done. Why red? Oh, red is pretty, bright and awesome – just like you. When we run this macro, it is going to color the currently selected cell with Red. Now that you understand some VBA jargon, lets move on and write our very first VBA Macro. Since we dont know any VBA, we will use this recorder to record our actions and then we will see recorded instructions (called as code in computer lingo) to understand how VBA looks like. Excel has a built-in tape recorder, that listens and records everything you do, in Excel’s own language, ie VBA. This is where Excel’s tape recorder will help us. In order to write your first VBA program (or Macro), you need to know the language first. Note: If you are new computer programming, watch our Introduction to Programming Video before proceeding. What is a Macro then?Ī macro is nothing but a set of instructions you give Excel in the VBA language. Thus enabling you to program Excel so that you can automate a boring report, format a big&ugly chart, clean-up some messy data or just play some random noises. When you tell instructions to Excel in this VBA language, Excel can do what you tell it. This language is called as VBA (Visual Basic for Applications). Just like you and I, Excel has a language too, the one it can speak and understand. You may be fluent in English, Spanish, French, German or Vietnamese. But I also speak Hindi, English and Cutish (that is the language my 2 year old kids speak).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |