A VBA code backup file is a saved copy of your macros that lives outside the workbook they run in, so a corrupted file, a lost drive or a botched edit cannot wipe out hours of work. The quickest reliable method is to export each code module to text files from the Visual Basic Editor (right-click a module and choose Export File), because .bas, .cls and .frm files reload cleanly into any workbook. Saving the workbook itself also backs up the code, but only if you use a macro-enabled format such as .xlsm or .xlsb. This guide covers both methods, where the files actually are, and what to do when a restore does not go to plan.

What You'll Need

  • Microsoft Excel or another Office app with macros enabled, and permission to run them (some workplaces block macros by policy).
  • A macro-enabled workbook: .xlsm, .xlsb, .xlam or .xls. A standard .xlsx file cannot store VBA code at all.
  • A place to keep backups that is separate from the original file, such as a second drive, a network folder or a cloud sync folder.
  • Optional: a free file archiving or versioning tool you already trust, so you can keep several dated copies.

Step-by-Step

  1. Confirm the workbook can hold code. Open the file and check the title bar. If it says .xlsx, code cannot be saved inside it. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm) before you go any further. If the document is shared or protected, ask the owner to remove the restriction; you cannot back up code you cannot open.
  2. Open the Visual Basic Editor. Press Alt+F11 on Windows. On a Mac, use Tools > Macro > Visual Basic Editor from the Excel menu bar, or the equivalent shortcut in your version. The editor opens in its own window with a Project pane on the left. Labels and layout can differ slightly by Office version and region.
  3. Expand the project tree. In the Project Explorer, find your workbook by name and expand the folders: Microsoft Excel Objects, Modules, Class Modules and Forms. Each entry under those folders is one code container.
  4. Export each module. Right-click a module, choose Export File, pick a folder dedicated to VBA backups, and keep the suggested file name or give it a clear one such as InvoiceTools_2026-01.bas. Repeat for every module, class module and user form. Do not skip the ThisWorkbook and sheet objects: sheet-level event code is easy to forget and painful to rebuild.
  5. Name the files so you can restore them later. Use the format ProjectName_Component_Date, for example Reports_Module1_2026-01-15.bas. Text files have no internal memory of which workbook they came from, so the file name is your only clue.
  6. Save a copy of the whole workbook too. Use File > Save As, choose .xlsm, and save it to your backup folder with a dated name. This captures everything at once, including code, formulas, formatting and references, and it is the fastest route back if a restore goes wrong.
  7. Copy the backup folder somewhere else. Drag the folder to a second physical drive, a network share or a synced cloud folder. A backup that sits on the same disk as the original disappears with that disk.
  8. Record the references. In the Visual Basic Editor, open Tools > References and note which libraries are ticked. Exported modules keep the code but not the ticked references, so you will need this list when restoring on a new machine.
  9. Test the backup before you need it. Create a blank macro-enabled workbook, open the editor, use File > Import File, and load one exported module. If it appears and compiles without errors, your backup is real. Delete the test workbook afterwards or keep it separate.
  10. Repeat on a schedule. After any meaningful code change, re-export the modules you edited and overwrite the dated copies, or add a new date. Weekly is plenty for most home and small-office workbooks; every session is better for active projects.

Common Mistakes

Saving as .xlsx is the single most common failure. Excel warns you that macros cannot be saved, many people click through it, and the code is gone when the file closes. Watch for that dialog and always choose Keep Macros or Save As .xlsm.

Another frequent error is exporting only standard modules. Class modules, user forms and the code behind worksheets and the ThisWorkbook object are separate components and each needs its own export. A backup missing them restores a workbook that half works.

Storing the backup in the same folder as the workbook, or on the same drive, defeats the purpose. So does keeping a single copy that gets overwritten after a bad edit, which turns your backup into a copy of the mistake. Keep at least two dated versions.

Finally, many people never test the backup. A folder of .bas files that has never been imported is an assumption, not a backup.

Tips

  • Store the reference list from Tools > References as a plain text note inside the same backup folder.
  • If you use a version control tool you already understand, a folder of exported .bas and .cls files is text and compares well between versions.
  • A personal macro workbook saved as .xlam also holds code, so include it in your routine.
  • Keep a short readme in the folder describing which workbook each module belongs to and the order to import them.
  • Before making a risky change, export first, then edit. The export takes seconds.
  • If macros are blocked by company policy, the backup will not run elsewhere either; check with whoever manages your devices.

Troubleshooting

The Import File option is greyed out or nothing appears. Make sure you are in the Visual Basic Editor, not the Excel window, and that you right-clicked a module folder rather than the project name. If the file extension does not match the component (a form file imported as a module), the import fails silently or shows an error. Import .frm forms as forms, .cls files as class modules, and .bas files as standard modules.

Imported code shows red errors. Missing references are the usual cause. Open Tools > References and tick the libraries your reference note lists. If a library is not installed on this machine, the code will not compile until it is installed or the code is rewritten to avoid it.

Macros disappear after reopening the workbook. Check the file extension first. If it is .xlsx, the code was never saved. If it is .xlsm, look at the message bar under the ribbon: macros may be disabled, and you may need to enable content or ask your administrator to allow the file. Also check that a shared workbook is not blocking macro storage.

Restoring over an existing project duplicated everything. Importing does not replace a module with the same name; it creates a second one with a numeric suffix, or refuses the import. Delete or rename the existing module first, then import, and test that the workbook still compiles with Debug > Compile.

The workbook is corrupted and will not open at all. Try opening it with macros disabled, then immediately export whatever code still loads. If the file will not open, restore your last exported modules into a fresh workbook and re-add references and sheet event code by hand.

Frequently Asked Questions

Where is VBA code stored in a workbook?

Inside the file itself, in a hidden binary part of the macro-enabled workbook. It is not a separate file you can open in Notepad, which is why exporting to text is the practical backup route.

Will saving the workbook as .xlsm back up my macros?

Yes. The code is stored in the workbook, so a dated copy of the .xlsm is a full backup. The weakness is that one corrupted copy loses code and data together, which is why exporting modules as well is better.

Can I back up VBA code automatically?

You can write a macro that exports modules on a schedule using the Export method, but that macro itself needs to be saved somewhere safe. For most users, a manual export before each change is simpler and more reliable.

Do exported files keep my macro security settings or references?

No. They keep the code only. References, passwords on the VBA project and workbook-level settings have to be reapplied after a restore.

Is it safe to store exported code in the cloud?

Yes, if your organisation allows it. Exported files are plain text and may contain data such as folder paths or connection details, so check your own policy before syncing them to a personal account.