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

    How do i change excel sheet1, sheet2,.... at the bottom of the screen, to appear as a navigation tab on top of the screen?

  2. Hello Alexander.

    Could you please let me know why the below code is not running when I click on Run button, there is no response nor error message populated.

    Sub LoopThroughDirectory()

    Dim MyFile As String
    Dim erow
    MyFile = Dir("C:\Users\r628956\Desktop\Salesforce\Rating\Automation\Master")

    Do While Len(MyFile) > 0
    If MyFile = "zmaster.xlsm" Then
    Exit Sub
    End If

    Workbooks.Open (MyFile)
    Range("A2:D2").Copy
    ActiveWorkbook.Close

    erow = Sheet1.Cells(Rows.Count, 1).End(x1Up).Offset(1, 0).Row
    ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range

    MyFile = Dir

    Loop
    End Sub

    Thank you - Rajkumar

  3. I m give information for vba programing

  4. Ripper, thanks for this

  5. Hello All .Very useful article. Would you be able to to help me for the following : i have a text in Excel worksheet that i want to copy and paste into Excel VB Standard Module . Currently I can do it using "Cntl C" to copy and then Cntl V " to paste into Excel VB Module . Can you please suggest some other method of how it can be done without pushing the above buttons.

  6. i m new to this excel and vba but i know vb how to work on this

  7. sry

  8. fhfghgvnvfdg

  9. Hi im not able to run 2 different modules at the same time using the spellnumber formula i want to be using it for 2 different currencies
    kindly advise

  10. hello, could anyone help me learn excel macro. i work on automation. im on my training period. and as of now my task is to create the popup calendar, meaning, i need to find people who hav joined on that particular date. i should get the popup calendar, when i click on a particular date, i must get the list of people who hav joined into the organization on that day.

    thanks.

  11. morning

    i copied your coding into my excel and saved it correctly, but when i open the file again it gives me an error. when i press alt F8 there are not macros on the list for me to chose from. when i open a new sheet and redo everything then it works but as soon as i save that file and open it i get the same result.

  12. Hello Alexander.

    Could you help me with the VBA code to complete the following task:

    I have an excel table. Assuming the formula goes in column D starting the cell D1,

    For i=1 To Countif(A:A,"*")
    Do Until True
    If Left(C1,B(0+i))=A(0+i) Then A(0+i)

    Here B(0+i) should start from B1,then B2, etc. until C1=A1 or A2 or etc.

    For D2 the VBA should do,

    For i=1 To Countif(A:A,"*")
    Do Until True
    If Left(C2,B(0+i))=A(0+i) Then A(0+i)

    Thanks

  13. I have visual program of timestamp whenever i enter sonething time will note....is it complusary to create a macro nd assign prog in it..? Der is no direct method to save dat prog in excel...?

  14. Can you tell which book is Best to learn macro in easy way to implement macros in excel**********

  15. Hi, everytime I save as on excel I get strange symbols characters after the file name this is for excel 2013.

  16. can u tell which book will be best to learn macro

  17. creat your own funcation -
    Access include many built -in ,or instrinsic ,funcation an intrust payment. you use these funcation to perform calculation without having to creat complicated expressions .using VBA, you can also creat your own funcation to either perfom calculation that excced the capability of an expressions or replace complex expressions you have written in your application.

  18. Hi,

    I have VBA code in Sheet3 column A of my excel file. I want to assign a button in sheet1 A1 to copy the vba code from sheet3 colum A and run the script.
    I am not able to put the code to open VBA through macro.
    Please help.

  19. Can please give information regarding
    what are the changes that we have to take into account while working excel VBA 2014??
    all my codes are written in VBA 2010 but code are not working in 2014 VBA ?? so please suggest me regarding this

  20. hi.
    i am working on excel workbook having 1000 worksheets.i want to extract data from all 1000 sheets in rangs(A6, A13:A15, A30:A34, A36:A40). Data present in workbook, are in the above said position in all of the worksheets. so someone provide me a macro for doing this thing.
    thank you for help

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