How to insert and run VBA code in Excel - 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 different Excel versions. 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
  3. 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
  4. 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

  5. Tip: 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).

  6. 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

  7. 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

256 comments

  1. Worked great! I didn't bother with any of the added code. Thanks!

  2. Can anyone tell me that how to run any of macro program in excel?

  3. I entered a VBA for excel 2013. I wanted to make a drop down list w/ multi items to select. It works perfectly for me, but when I send it to my associate it does not work...only single selection is possible. Is that because they do not have a, "developer" ribbon? I cannot have everyone do that....so is there a workaround? Thanks for your valuable time!

  4. thank you sir for treating my worries

  5. how do i run a vba code using a macro such that each time i click on the macro, it runs my vba code

  6. Thanks for the Aritical.

  7. Delete entire row from A2 downwards with the word "0o,rb,t"in column A

  8. Hello,

    I am trying to import data from certain columns from an excel sheet "MyData.xlsx" into "PriceInc.xlsx"

    There is a button on the "PriceInc.xlsx" file that, when clicked should bring in the required data.

  9. Hi , I just started with VBA using it for the first time , I wrote code under module but When I am clicking on "Run".
    It is not giving me any result.

    Suppose I use a command :
    Sub hello()
    MsgBox ("Helloworld")
    End Sub

    When I am running it , it is not getting published in Excel workbook

    • Hi, khush

      Sub hello()
      MsgBox "Helloworld"
      End Sub

  10. Plz Send All details With Screen Shot.that how to create VBA in Excell Sheet

  11. Thanks!

  12. I am opening a "shared" spreadsheet, that sits on our Corporate NAS. I have copied the file off the NAS to my home computer, in order to add some VBA code - for displaying the sheet and refreshing at set intervals. However, when I open the Developer tab and attempt to right click on the sheet, all my options are greyed out? Is this because the sheet has been placed in "shared mode" from within Excel (not sure how to disable that and add the code, then re-enable shared mode). I will need to put it back on the NAS when the code has been added. NB: The VBA code was produced by a freelancer at guru.com to refresh each of the four tabs on the sheet, every 15 seconds.. Thanks! (I have a screen capture I could send you).

  13. Thank you, Alexander.

    You have solved my problem.

  14. I want to create a data base using excel i want to create it where if i search the item the prices and purchased store will appear

  15. Hi i have used the line break formula, and it is working great.

    Further please help in make macro to break line. As in if there are double spacing in between two words, then that should come in the next line of the cell.

    Thanks

  16. Hi,
    I want to put a macro to getting the first line bold on the selected column

    Please help!

  17. I created a little ActiveX control (spin up/down)via VBA for my PPT chart. But I can't find anywhere in the videos how to save it in order to use it. Can someone please tell me how or direct me where to go? Thanks.

  18. Hi ,

    Can you please share a copy of a worked file on which macro is created and run to my mail Id as in shared. just to take it as a example and work with

    Regards,
    Chandini.E

  19. I would like to create a macro where you go through the steps to insert a picture into a comment, but i would like to have the macro stop at the insert picture screen so it is simple for whoever is using the document can go directly to the insert a picture popup and finish the request.

  20. Sir,
    now, i want to make Dashboard to Summarise the Sales data, Return, & Stock , Date wise transaction & Unique Column is SKU Code, kindly help me how to process it on every 7 Days, if you have any templet as our Requirement , then pls provide.

    Thanks
    ashish

Post a comment



Thank you for your comment!
When posting a question, please be very clear and concise. This will help us provide a quick and relevant solution to
your query. We cannot guarantee that we will answer every question, but we'll do our best :)