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. This is excellent! I work at an I-Bank this is gold! Thank you Sir.

  2. Hi. I'm using a macro to combine multiple sheets in one excel workbook. Macro works but I keep getting a screen that asks me if I want to save or dont save I want to add this step into the macro, can someone please help?
    Thank you in advance.

    Sub GetSheets()
    Path = "\\>>>>>>>>>Extract for CD 20171027\"
    Filename = Dir(Path & "*.xls")
    Do While Filename ""
    Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
    For Each Sheet In ActiveWorkbook.Sheets
    Sheet.Copy After:=ThisWorkbook.Sheets(1)
    Next Sheet
    Workbooks(Filename).Close
    Filename = Dir()
    Loop
    End Sub

  3. Thank you so much! I've never added a macro and the steps were super easy to follow. Easy fix.

  4. how to use a module in a userform more than textboxes in same module
    please given the code and how to create a time symbol provide a number format example (1230 =12:30) in this code give to me
    thanks you

  5. bvfhj

  6. Hi,

    Please help me. I want to color cell background by comparing with cell value and one more cell value. Example: Col A (1:10) has values 0 & 1. Col B:E (B1:E10) has values of 1-100. Color B1:E10 if the value of cell >50 & A1 equal to 0. Total four colors and four conditions.
    1) A1:A10 = 0 then B1:E10 =50
    3) A1:A10 = 1 then B1:E10 =50

    I am able to create only for if B1:E10 >/<50.

    Thanks,
    Ravi

  7. I went through the steps above to add VBA code to my spreadsheet, however, when opening the macro, there is nothing listed for selection. Why is that? (In other words, I can see the information through ALT + F11, but when selecting ALT + F8, the macro is not there).

  8. I want to save a running total from a cell in Excel and wondered can this be done? I have a formula to put the total in a cell on a certain date but when the date has passed it clears the cell again and I want the running total on that date to be captured. My formaula in excel to capture data on a certian date is:- =IF(D2=(DATE(2017,6,11)),D3,0)

    Thanks for any info, Cheers Trev

  9. Hi
    Can this code be applied to all worksheets? if yes help me make it permanent in excel. I tried to save as a add-in but its not working all workbooks.

  10. Hi

    How to run macro in MS Task scheduler, please help me.

  11. Hi,

    I am trying to create a button in excel that when clicked it, will add 1 point to a score on different sheet (sheet 2). I can't seem to find out if this is possible, and if it is, how to do it.

    Ideally I want to have multiple buttons so I can create scoring charts (team 1's score in B1, team 2's score in B2, etc) but you won't be able to see it until you go on to sheet 2.

    Any help would be gratefully received!

  12. A notification bar appears which reads:
    Be careful! Parts of your document may include personal information that cannot be removed by the Document Inspector

  13. Is there a way to have this constant and not having to run the Macro for it to be applied?

  14. Hi sir
    I want VBA code for to display the excel data in text box. For example 3 text boxes in VBA form in excel a1,b1,c1 have data. If a1 and b1 data typed in text box 1 and 2 c1 data will display in textbox3.

  15. Hi Alexander,

    Please help me out when i use VBA coding for Consolidated Data show this error.

    ---------------------------
    Microsoft Visual Basic for Applications
    ---------------------------
    Compile error:

    Sub or Function not defined
    ---------------------------
    OK Help
    ---------------------------

  16. I've followed all the steps, but I'm having a problem with the code doing what I need. Instead of adding the dollar amounts in the cells that have a specific colored background, I'm getting how many cells have that colored background. Example: 5 green cells with $20 in each of them, the code gives me $5 when it should be $100.

  17. I need to Increment 50 by 0.64 everyday at 8:00 AM and I would like to automate it. I've already have a Now function that updates every second, I also need a way to start it when Excel is opened.

  18. Where/how do I see the results, after I run the code above?

  19. it is way too complicated i just want a print screen macro

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 :)