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. Works perfectly! Thanks!

  2. I was wondering how I can modify the code to have all the excel files into 1 sheet instead of each individual sheets? Thank you

  3. Hello, i would like to change the address from where i am sending all the emails using the VBA macro excel. How do i change that? i dont know i am clear o not. Every time i send the emails from the VBA macro i would like to that "sender" appears a different email address than from where i am really am sending from.

  4. I am new so hope you can help.
    I would like to write code for registration of players and teams. How do I start please?
    Thanks
    John

  5. I want to develop a VBA code; let us suppose we have values like 1 2 3 4 5 in col A1 A2 A3 A4 A5 and other values like 6 7 8 9 10 in col B1 B2 B3 B4 B4 B5 and I want to write 1/6 2/7 3/8 4/9 5/10 in columns C1 C2 C3 C4 C5.

  6. Is it possible to call excell or word, without needing to specify which version (ie word 14, word 15 etc).

  7. in vba modules are saved by which extension

  8. Hi
    I am wanting to make a Commond Button that will insert a new row below the active cell and in the new row have formulas automatically populated.
    ie say active cell is D8 the press the Button and a new row is added to Row 9 which now includes the formula =sum(D7+B6) in the new cell C9 and the formula =sum(a2-a5) in cell e9.

    Thank

  9. Hi
    please I need assistance in converting the word into numbers

    example: let say the word (Rawad) and we have R = 10 , A = 1 , W = 700 and D = 3

    so the total of word Rawad must be 10 + 1 + 700 + 1 + 3 = 715

    thanks in advance

  10. Sir please forward Ur good comments that how I easily access what kind of book in Pakistan for learning VBA codes.

  11. How I made search button in excel VBA

  12. Hi - I am trying to create a Button in my sheet for a randomizer formula I set up. Basically, I have a list A:A of SKU names and B:B Random(1,5000)
    I want the button to auto-generate a 10 item SKU list that can be used for Inventory Cycle Counting. I set up the button but can not get any further with the visual basic script (newbie)

  13. dear sir\madam
    please tell me how to learn vba macros coding in excel sheets
    please help me

  14. I am failing to locate and make the Vb code

  15. Hi I cannot figure out how to have a row of possible 100 numbers that when I enter a number in cell A1 all the numbers move up one cell and the new number appears in B1 any help would be appreciated

  16. Hi,
    If i run the macro its successfully run ,while the next time i get an error . Kindly help what are the comment i need to use

  17. I performed the task but my spreadsheets did not merge what did I do wrong? All of the examples I followed but after the Alt F8 I was lost because it did not show what to do next or what I should be looking for.
    thanks

  18. Is import function runs in excel 2010? previously i created a code using import function to import .bas file into many excels sheets by running this code. now this in not working for excel 2010. can anybody please help me out here?

    • Code:
      ActiveWorkBook.VBProject.VBComponents.Import ("C:\.....code.bas")

  19. I only need to remove carrigae returns in 1 column in my case Column H.

    How can I define the range to be H, and not the whole sheet?

    Thanks

  20. can you please tell how to copy paste from row1(sheet1) to any cell in sheet2.. and create a button for going to next row in sheet1

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