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 Excel 2010, 2013 and 2016. 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

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

    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

    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

You may also be interested in:

129 Responses to "How to insert and run VBA code in Excel - tutorial for beginners"

  1. Fernanda says:

    Hi, I added a VBA code for a new Formula (CONCATENATEIFS) and it works greate but the only problem is that its super slow, I tried to use your speed up tip but it ended up slower (probably because I dont know where exactly to put it because the formula doesn´t have a Sub line) Can you help me?
    The formula is this one: Function ConcatenateIfs(ConcatenateRange As Range, ParamArray Criteria() As Variant) As Variant
    ' Source:, August 2014
    Dim i As Long
    Dim c As Long
    Dim n As Long
    Dim f As Boolean
    Dim Separator As String
    Dim strResult As String
    On Error GoTo ErrHandler
    n = UBound(Criteria)
    If n < 3 Then
    ' Too few arguments
    GoTo ErrHandler
    End If
    If n Mod 3 = 0 Then
    ' Separator specified explicitly
    Separator = Criteria(n)
    ' Use default separator
    Separator = ","
    End If
    ' Loop through the cells of the concatenate range
    For i = 1 To ConcatenateRange.Count
    ' Start by assuming that we have a match
    f = True
    ' Loop through the conditions
    For c = 0 To n - 1 Step 3
    ' Does cell in criteria range match the condition?
    Select Case Criteria(c + 1)
    Case " Criteria(c + 2) Then
    f = False
    Exit For
    End If
    Case "= Criteria(c + 2) Then
    f = False
    Exit For
    End If
    Case ">="
    If Criteria(c).Cells(i).Value "
    If Criteria(c).Cells(i).Value <= Criteria(c + 2) Then
    f = False
    Exit For
    End If
    Case ""
    If Criteria(c).Cells(i).Value = Criteria(c + 2) Then
    f = False
    Exit For
    End If
    Case Else
    If Criteria(c).Cells(i).Value Criteria(c + 2) Then
    f = False
    Exit For
    End If
    End Select
    Next c
    ' Were all criteria satisfied?
    If f Then
    ' If so, add separator and value to result
    strResult = strResult & Separator & ConcatenateRange.Cells(i).Value
    End If
    Next i
    If strResult "" Then
    ' Remove first separator
    strResult = Mid(strResult, Len(Separator) + 1)
    End If
    ConcatenateIfs = strResult
    Exit Function
    ConcatenateIfs = CVErr(xlErrValue)
    End Function

    Thank you very much!!

  2. Sohaib says:

    Hi this is Sohaib. I have learned the VBA for converting numbers into words but I also want to add the word "only" after converting the numbers into words. Please help if you can.

  3. Nurul Islam Chowdhury says:

    Excellent guideline. Thanks.

  4. Sophie says:

    Thank you so much. Big help.

  5. Yana Nina says:

    If i can't save file after i add this VBA code. It says that there is an error and Excel may able to save file by removing or repairing some features. Options are to click continue or cancel. When clicking continue it want to save file as another one, but in the end it says :file was not saved".

  6. PeterF says:

    Thank You! Big help!

Post a comment

Unfortunately, due to the volume of comments received we cannot guarantee that we will be able to give you a timely response. When posting a question, please be very clear and concise. We thank you for understanding!
Excel add-ins and Outlook tools -
Ultimate Suite for Excel Professionals
60+ professional tools for Excel 2016-2007 to do your daily work
Incredible product, even better tech support…AbleBits totally delivers!
Deborah Bryant
Anyone who works with Excel is sure to find their work made easier
Jackie Lee
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
AbleBits suite has really helped me when I was in a crunch!
Nelda Fink
I have enjoyed every bit of it and time am using it
Christian Onyekachi Nwosu
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
Sheila Blanchard