Excel VBA can zip and unzip files by calling Windows' built-in compressed-folder shell object — there is no native ZIP function in VBA itself. The short working set is: add a reference to "Microsoft Shell Controls and Automation" (or late-bind the Shell.Application object), then use the CopyHere method on the Namespace of a folder to move files into a .zip and the same method on the zip's own namespace to pull them out. Once that object is in place, zipping a workbook's output files or unpacking a downloaded archive becomes a few lines of macro code.
This is the practical approach for automating backups, packaging reports for email, or reading files out of an archive that arrives weekly. It works in Excel on Windows. On macOS the Shell.Application route is not available, so the steps below apply to Windows only — Mac users need a different route, covered near the end.
What You'll Need
- Excel on Windows (desktop version), with macros enabled in a workbook saved as
.xlsm - Access to the Visual Basic Editor (Alt+F11)
- The files or folders you want to compress already existing on disk; VBA copies existing items, it does not generate them
- Basic familiarity with the VBA editor and how to run a macro
- Write permission in both the source and destination folders
Menu labels and dialog wording in Excel and Windows can differ slightly by version, edition and region, so treat names like "Trust Center" or "References" as the standard wording and look for the nearest equivalent on your build.
Step-by-Step
- Open the editor. Press Alt+F11 in Excel to open the Visual Basic Editor. If you prefer, go to the Developer tab and choose Visual Basic. The Developer tab may be hidden; enable it via File > Options > Customize Ribbon and tick Developer.
- Create a module. In the Project Explorer, right-click your workbook, choose Insert > Module. Code placed in a standard module can be run directly with F5.
- Decide between early and late binding. Early binding gives you IntelliSense: in the editor go to Tools > References and tick "Microsoft Shell Controls and Automation". Late binding needs no reference and is more portable — you create the object with
CreateObject("Shell.Application"). Late binding is the safer choice if the macro will run on other machines. - Write the zipping routine. The pattern is: create the Shell object, get the folder you want to add files from with
Shell.NameSpace(sourceFolder), get the target zip withShell.NameSpace(zipFilePath), then loop the source folder'sItemsand calltargetZip.CopyHere sourceItem. Note that CopyHere takes the item object, not a plain path string. - Create the zip file first if it does not exist.
NameSpacewill only open an existing folder or zip. The simplest reliable trick is to write an empty file with the.zipextension — VBA'sOpen path For Output As #1 : Close #1does this — and Windows will treat it as an empty compressed folder once the Shell reads it. Some Windows builds refuse to open a zero-byte zip until something is written, so test with your own files. - Handle the asynchronous behaviour.
CopyHerereturns before compression finishes. If you immediately try to move, send or delete the zip, you may hit a "file in use" error. Add a short wait loop — for example aDoloop withApplication.WaitorSleepafter declaring the API — or check that the zip's file size has stopped changing. - Run and verify. Save the workbook, place the cursor inside the Sub, and press F5. Open the destination folder in File Explorer and confirm the zip contains the expected files and that each one opens.
- Write the extraction routine. To unzip, get the archive's namespace with
Shell.NameSpace(zipPath), get the destination folder withShell.NameSpace(destFolder), then loop the archive'sItemsand calldestFolder.CopyHere archiveItem. Extraction preserves the internal folder structure if you copy the top-level items rather than individual files. - Add a guard for the destination. Check that the destination folder exists before extracting;
NameSpacereturns nothing otherwise. UseDir(destFolder, vbDirectory)to test, and create the folder withMkDirif needed.
Common Mistakes
- Passing a path to CopyHere.
CopyHereexpects a FolderItem object, not a string. Passing "C:\file.txt" silently does nothing useful; you must pass the item fromNameSpace(...).Items. - Forgetting to create the zip. Calling
NameSpaceon a non-existent.zippath returns nothing and the macro fails on the next line. Create the file first. - Assuming the zip is finished. Because CopyHere runs in the background, code that deletes the source files straight after the call can corrupt the archive. Wait for the operation to settle.
- Zipping the wrong level. Copying a folder adds the folder itself (with its contents) to the zip. To put the files at the zip root, copy the items inside the folder instead.
- Using early binding in a shared file. Other users may not have the Shell reference, producing a compile error. Prefer late binding for workbooks that travel.
- Expecting it to work on Mac. Shell.Application is a Windows component; the same code fails on Excel for Mac.
Tips
- Turn off screen updating and the status-bar noise with
Application.ScreenUpdating = Falsearound the loop, then restore it at the end. - Use
ThisWorkbook.PathandEnviron("TEMP")to build paths instead of hard-coding drive letters, which break when the file is moved. - Timestamp the zip name with
Format(Now, "yyyymmdd_hhmmss")so repeated runs never overwrite older backups. - Keep source files in a dedicated staging folder and zip from there, so a failed run leaves the originals untouched.
- If you need maximum control — partial selection, overwrite prompts, per-file handling — the Shell object can be replaced by a call to PowerShell's
Compress-ArchiveusingShellorWScript.Shell. That gives you an alternative if the Shell method misbehaves on your machine.
Troubleshooting
Nothing gets added to the zip. Confirm the destination .zip file existed before the call, then check that your loop is actually iterating Items and not a string array. Add a debug line printing each item's Name to the Immediate window.
"Path not found" or "file not found". Verify the exact path including the extension. Windows hides known extensions by default, so the real file may be named archive.zip.zip. Show file extensions in File Explorer to check.
The macro hangs or the file is locked. The compression is still running. Do not delete the source or the zip until it finishes; add a wait loop as described above. Close any File Explorer window that has the zip open, since an open preview can hold a lock.
An error about the object or the reference. If early binding is in use, check Tools > References for a MISSING entry against the Shell library. Switching to late binding usually clears it. If you get a permission error, the folder may be read-only or in a protected location — work in a normal user folder.
If that did not work, try a single-file manual test: in File Explorer, right-click one file, choose Send to > Compressed (zipped) folder, and confirm Windows can create a zip there at all. If it cannot, the problem is the folder permission or a blocked location, not the VBA. As a last resort, restart Excel and rebuild the module in a fresh workbook to rule out corruption.
Frequently Asked Questions
Can VBA create a password-protected zip?
Not with the Shell method — it creates ordinary, unprotected archives. Windows' built-in zipping has no password option. For encryption you need a third-party tool that accepts command-line switches; any password you set this way must also be stored somewhere safe, because you cannot recover it later.
Does this work in Excel for Mac?
No. Shell.Application is a Windows-only component. On Mac, options include the zip command-line tool invoked through Shell if it is available in your environment, or a third-party compression app. The steps here are Windows-specific.
Can I zip files that other programs have open?
Sometimes, but the result may be an incomplete or unreadable copy. Close the files first wherever you can. Excel workbooks that are open in the same instance especially need saving, since the archive copies whatever version is currently on disk.
Why does my extracted file have folders I did not expect?
Because the archive stores relative paths inside it. Copying the top-level items reproduces the folder tree. If you want flat files, extract them and then move or rename as needed — the Shell object does not flatten structure automatically.
Is there a size limit for zipping through VBA?
Limits vary by file system and free disk space rather than by VBA itself, so very large archives need patience and free space on the destination drive. Check that the drive has room for both the uncompressed files and the archive.
Can I run the macro automatically when the workbook opens?
Yes, but think twice. Code that runs on open can be flagged by Excel's security settings and may surprise users. If you automate it, keep the action obvious (for example, a clearly named button on a sheet) and avoid deleting anything without confirmation.