56 free macros indexedAll toolsHow to runBlogGitHub ↗

How to Add a Macro to Excel from a .bas File

To add a macro to Excel from a .bas file, press Alt + F11, right-click your project, choose Import File, pick the .bas, then save as .xlsm.

2026-09-08

How to add a macro to Excel from a .bas file

To add a macro to Excel from a .bas file, press Alt + F11 to open the Visual Basic Editor, right-click your workbook in the Project window, choose Import File, select the .bas, then save the workbook as .xlsm.

Almost every guide to this question explains how to write your first macro from scratch. That is a different job. This one is about code somebody else already wrote, sitting in a file on your computer.

TL;DR: Key takeaways

  • Alt + F11, right-click the project, Import File, choose the .bas.
  • Import keeps the module name and needs no cleanup.
  • If you paste the code instead, delete the Attribute VB_Name line or Excel throws a syntax error.
  • Save as .xlsm afterwards. Saving as .xlsx discards the macro.
  • Put it in PERSONAL.XLSB to make it available in every workbook.

Import the .bas file

This is the reliable route, because Excel reads the file exactly as it was exported.

  1. Open the workbook that should hold the macro.
  2. Press Alt + F11 to open the Visual Basic Editor.
  3. In the Project window on the left, right-click your workbook's name.
  4. Choose Import File.
  5. Select the .bas file and click Open.

The macro appears under Modules, already named. Close the editor and press Alt + F8 to run it.

You do not need the Developer tab for any of this, though enabling the Developer tab gives you a button for the editor if you prefer clicking to keyboard shortcuts.

Or paste the code, minus one line

Pasting works too, and it is what most people try first. There is one trap.

A .bas file begins with a line like Attribute VB_Name = "RemoveBlankRows". That line is part of the file format, not part of the code. The Visual Basic Editor writes it on export and reads it on import, but it is not valid to type into a code window. Paste the whole file and Excel stops with a syntax error on that first line.

So paste it this way:

  1. Open the .bas in any text editor.
  2. Copy everything except the Attribute VB_Name line.
  3. In the editor, choose Insert > Module.
  4. Paste the code into the blank module.

The only thing you lose is the module name, which Excel replaces with Module1. The macro itself runs identically. Importing avoids the whole problem, which is why it is the better habit.

Every macro in our free tools library is published as a plain .bas in a public repository, so you can read the code before importing it. The Remove Blank Rows macro is a reasonable first one to try, since its behaviour is easy to check against a copy of your data.

Save as .xlsm or lose the macro

Once the module is in, the workbook has to be saved in a macro-enabled format.

Go to File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). If you save as .xlsx, Excel warns that the VBA project cannot be kept, and the macro is gone from the saved copy. XLSM vs XLSX covers when each format is the right choice.

Make it available in every workbook

Importing into one workbook ties the macro to that file. To use it everywhere on your machine, import it into your Personal Macro Workbook instead.

In the Project window, look for PERSONAL.XLSB and right-click that rather than your own workbook, then import as normal. If it is not listed, record any throwaway macro once and choose Personal Macro Workbook as the destination. Excel creates the file, and it opens hidden every time Excel starts.

Frequently asked questions

What is a .bas file?

It is a plain-text export of a VBA module. Excel produces one when you right-click a module and choose Export File, and reads one back with Import File. You can open it in any text editor, which is what makes it a sensible way to publish and review macro code.

Why do I get a syntax error when I paste VBA code?

Almost always because the pasted text still contains the Attribute VB_Name line from the top of a .bas file. That line belongs to the file, not the code window. Delete it and the rest compiles, or use Import File instead and avoid the problem.

Where does the imported macro appear?

Under Modules in the Project window, named after the module rather than the file. To run it, close the editor and press Alt + F8 on Windows or Option + F8 on Mac, then pick it from the list.

Can I import a .bas file in Excel for the web?

No. Excel for the web cannot create, run, or edit VBA macros, so there is no editor to import into. Use desktop Excel on Windows or Mac, then reopen the file in the browser if you need to.

Is it safe to import a .bas someone sent me?

Only if you have read it. A .bas is plain text, so open it in a text editor first and see what it does. Code you cannot read, from a source you did not choose, is exactly what the macro security warnings exist for.

What to do next

Import one macro, run it on a copy of a real file, then open it with Alt + F11 and read what it does. Editing or deleting a macro covers changing it afterwards, and running a macro you downloaded covers the simpler case where the macro already ships inside a workbook.