Dec
6

How to insert and run VBA code in Excel 2010, 2013 - tutorial for beginners

This is a short step-by-step tutorial for beginners showing how to add VBA code (Visual Basic for Applications code) to your Excel workbook and run this macro to solve your spreadsheet tasks.

Most people like me and you are not real Microsoft Office gurus. So, we may not know all specificities of calling this or that option, and we cannot tell the difference between VBA execution speed in Excel 2010 and 2013. We use Excel as a tool for processing our applied data.

Suppose you need to change your data in some way. You googled a lot and found a VBA macro that solves your task. However, your knowledge of VBA leaves much to be desired. Feel free to study this step-by-step guide to be able to use the code you found:

Insert VBA code to Excel Workbook

For this example, we are going to use a VBA macro to remove line breaks from the current worksheet.

1. Open your workbook in Excel.

2. Press Alt+F11 to open Visual Basic Editor (VBE).
Excel Visual Basic Editor window

2. Right-click on your workbook name in the "Project-VBAProject" pane (at the top left corner of the editor window) and select Insert -> Module from the context menu.
Insert a new VBA module to the Excel workbook

3. Copy the VBA code (from a web-page etc.) and paste it to the right pane of the VBA editor ("Module1" window).
Paste the code to the VBA module

Tip: How to speed up macro execution.
If the code of your VBA macro does not contain the following lines in the beginning:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Then add the following lines to get your macro to work faster (see the screenshots above):

  • To the very beginning of the code, after all code lines that start with Dim (if there are no "Dim" lines, then add them right after the Sub line):
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
  • To the very of the code, before End Sub:
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

These lines, as their names suggest, turn off screen refresh and recalculating the workbook's formulas before running the macro.

After the code is executed, everything is turned back on. As a result, the performance is increased from 10% to 500% (aha, the macro works 5 times faster if it continuously manipulates the cells' contents).

4. Save your workbook as "Excel macro-enabled workbook". Press Crl+S, then click the "No" button in the "The following features cannot be saved in macro-free workbook" warning dialog.
The following features cannot be saved in macro-free workbook

The "Save as" dialog will open. Choose "Excel macro-enabled workbook" from the "Save as type" drop-down list and click the Save button.
Save your workbook as Excel macro-enabled workbook

5. Press Alt+Q to close the Editor window and switch back to your workbook.

How to run VBA macros in Excel

When you want to run the VBA code that you added as described in the section above: press Alt+F8 to open the "Macro" dialog.

Then select the wanted macro from the "Macro Name" list and click the "Run" button.
Press Alt+F8 to run the VBA macro

21 Responses to "How to insert and run VBA code in Excel 2010, 2013 - tutorial for beginners"

  1. Hemanth Kumar C says:

    Hi,

    I required a VBA Code for Updating a Bulk excel into a Application(Oracle) Data batabase & to Execute the same through the excel to Application.

    Ex :- The Complaint is to be closed in the system for this there are 4 Columns 2 are of Drop down & 1 is of Text field to Update a Remark, & in the 4th Column Present Date & Time will be appearing that has to remain Same.

    Regards,
    Hemanth C
    9449330057

  2. syed says:

    Hello There,

    Iam not able to click on module,i can see it but not able to click, can anyone help on this.

    Regards
    Syed

  3. Jim says:

    Do you have any idea why some VBA code that works fine in Excel 2010 would lead to a "Microsoft Excel has stopped working" error in Excel 2013?

    Thanks,

    Jim

  4. Jim says:

    Thanks for the reply. Any suggestions for why that macro may not work in 2013?

  5. Angel says:

    hello,
    when I copy and paste the formula (the one found in how to count by color and sum by color in an exel worksheet tutorial)into the module of my worksheet - it does not look the same. the letters are red and instead of line by line - it looks like it was a continuous formula (actually took up only 2 lines)

    so anyways - i tried to run it (alt8) and it is black - it seems like it does not recognize the formula I pasted.

    so I cut and pasted the formula into word document - and it took a few pages - and when I cut and copied it from there - it showed a solid line where the I copied onto the next page)

    any suggestions? am I doing some thing wrong?

  6. Angel says:

    I have cut/paste the formula once again in MS Word and then from there - copy/paste into module. It now shows up normal (rather than the 2 red lines of words in my previous post)

    so, now that its there...
    1. I saved the workbook as excel macro-enabled workbook
    2. Clicked ALT Q - to go back to my workbook
    3. I then ALT F8 to run it - but there is nothing there... blank....

    what am I missing? At this point - do I run the macro BEFORE I try to do what I want accomplished (uie: add sum of each yellow cell)?
    I think I am missing a step here. Your tutorial seems pretty well explained. I was very pleased and thought I understood it.... but I feel like Im missing a step somewhere....
    Thank you for your help.

  7. Robert says:

    I may be missing something, but even after pasting some code in, there is no macro listed when I use Alt+F8 - the window is blank.

    What step did I miss (that is not listed on this page?)

    • Alexander says:

      Hi Robert,
      I described all the needed steps in the article. Please make sure that the code you pasted contains the part beginning with Sub MacroName and ending with End Sub.

  8. Help says:

    I need help badly, I need macro-excel where:
    1) multiple rows of surnames in one column and firstnames in another column
    2) have to copy each name surname and first name to be pasted website asking for each in separate webpage boxes
    3) then I have to copy the printscreen result to another worksheet and resume to another row (new firstname and surname)
    Need a as this is a big project but few people/time
    Thanks, Alexander!

  9. Kumarapush says:

    Hai Alexander, I know bit of Macro, but the below lines are new to me.
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Very Interesting to know about this TIP, that it will increase the execution to a great extend. Thanks for the Info. :-)

  10. How to create a macro says:

    Hi,

    I prepare a report everyday, can it not be done with the help of macro.
    I downlaoad the raw report it from my client system and then format it and I use some simple formulae like sum, vlook up.

    Everyday the raw date keeps changing.

    please suggest at the earliest, how this can be done.

    Thanks,
    Rumana

  11. parvin says:

    Hi, I want to link some pdf file data to excel using macro.Is it possible?

  12. sanjeev says:

    I am sanjeev .I am new in excel....so could u plzzzzzzzzz help me to sove my problem????????

    I write ,i code in VBA in excel...........
    to move from one sheet to onther by active x control command button.

    command button in first sheet1............

    worksheets("sheet2").activate

    it works..........by this i can go ,into second sheet.
    but as i close my sheet ,and reopen it ,it not works...........(means code disappears).

Post a comment



Ultimate Suite for Excel Professionals
 
 
The best spent money on software I've ever spent!
Patrick Raugh
Ablebits is a fantastic product - easy to use and so efficient.
Debra Celmer
Excel is at its best now
Annie C.
I don't know how to thank you enough for your Excel add-ins
Jennifer Morningstar
Anybody who experiences it, is bound to love it!
Kumar Nepa
It's the best $100 we've ever spent!
Mike Cavanagh
I love the program, and I can't imagine using Excel without it!
Robert Madsen
One word… WOW!
Dave Brown
Love the products!
David Johnston
It is like having an expert at my shoulder helping me…
Linda Shakespeare
Your software really helps make my job easier
Jeannie C.
Thanks for a terrific product that is worth every single cent!
Dianne Young
I love your product
Brad Gibson
Awesome!!!
Sheila Blanchard