How to Creat VBA - Write a Simple Macro in Excel

In this Article, we'll see how to write a very simple and basic macro. 

If you're not a developer if you're looking this VBA code for the first time how to write it let us see that now I am in the same Excel VBA workbook, and I'm in the sheet I've created be underscored write a macro, so it's a blank sheet which is created now if you want to write a macro what are the steps very first thing is you have to open the VBA editor Visual Basic for application editor, so I'll just go to this Developer tab and if you see there's a Visual Basic given here I'll click on this Visual Basic this macro if you see this is a window for VBA editor in previous video we have recorded with macaron this was the macro which you have recorded now that has been rented in this model part if you double-click on this place this is the module editor now if you want to write a simple macro first create here a module okay how do you create it very simple if you go to this insert menu bar you see there's a model given here click on this module ok now this model is given as model 2 know what we'll do is I'll change the name of this model so for that you have to go to this view properties window related to this model these are the properties so here you can go and change the name I'll write as B underscore write a macro B underscore right macro a simple macro that's it so if you see here the name has changed B underscore write a macro now if you want to write a macro anything any coding part can be written in sub procedure so make sure you write a sub part you write a sub prod, and I'll write this first macro open and close bracket enter, so this is a sub procedure which I've written first 2 macro is a name if you want to get change the name also that's not a compulsion and finally n sub will automatically come here ok that's it now you can keep on writing whatever you want in between this place so now what I want is I want to display some text in the form of message box, so there's a code which you can write here as message box msg Buick and in double quotes you can write the text remember whenever you are writing a text you have to write in double quotes I'll write as tutorials point that's it so that's a single line which I've written if you want to exude the code which is written here you have to just keep your cursor in between this sub and ends up, and you can just click on this Run button okay audience you can go to this menu bar, and you can see run menu bar this first option is given as run or else the shortcut f5 from a keyboard, so I'll just click on this Run button and if you observe I've got this dialog box that is known as message box and this is nothing but tutorials point which is written here, and I'll just click on OK so once you click on OK it will come back to the VBA editor suppose you don't want to display a text you want to display some number it's very simple you just type as message box, and you'll type yours number 100 that's it so message box a hundred and again if you click on run first line will be executed it will display a message box second again a message box I'll just click on this run okay once I click on this run it says which macro do you want to run do you want to run this color macro or the first macro I'll say I want to run this first macro this is the name first macro click on run now if you observe first thing I've got as tutorials point second thing I'll get this number as 100 so that's very simple message box which you can display, so you can keep on writing different text and with methods you'll get a confidence how to write a very simple and basic macro I hope you have understood and that's all for this video

Enjoyed this article? Stay informed by joining our newsletter!

Comments

You must be logged in to post a comment.

About Author