This post may contain affiliate links. Please read my disclosure for more info.

VBA code to Zip and Unzip files and folders

VBA Code Snippets

VBA Code Snippets

Recently, I have been working on a project which requires the zipping and unzipping files and folders.  Zip files are now a common method of compressing files and folders for sharing.  As software becomes more complex, file sizes increase, however there is often a limit to the file size an e-mail provider will allow.  For example, Google currently allows a maximum file size of 25MB to be sent.  Putting all the attachments into a single zip file can help get around this issue, as the files are compressed to be smaller.

The code snippets below are based on a section from Excel 2016 Power Programming with VBA by Michael Alexander/Dick Kusleika and from Ron de Bruin’s site.

Whilst working with Zip files, I wanted to make a reusable procedure which I could call when ever required.  The code below was created for that purpose.  These code snippets do not create, delete or check for the existence of the files or folders which it uses.  Check out the following code snippets to cover these areas:


UPDATE: As discussed in the comments section below.  Do not declare a String variable to hold the file paths, this will not work with the Shell.Application.  Declare a Variant variable to hold the file paths, this will ensure the code runs smoothly.


Create a zip file from a folder

This procedure has only a few steps:

  1. Create an empty zip file
  2. Copy the files from the folder into the zip file
  3. Wait for all the zip files to stop compressing
Sub CreateZipFile(folderToZipPath As Variant, zippedFileFullName As Variant)

Dim ShellApp As Object

'Create an empty zip file
Open zippedFileFullName For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1

'Copy the files & folders into the zip file
Set ShellApp = CreateObject("Shell.Application")
ShellApp.Namespace(zippedFileFullName).CopyHere ShellApp.Namespace(folderToZipPath).items

'Zipping the files may take a while, create loop to pause the macro until zipping has finished.
On Error Resume Next
Do Until ShellApp.Namespace(zippedFileFullName).items.Count = ShellApp.Namespace(folderToZipPath).items.Count
    Application.Wait (Now + TimeValue("0:00:01"))
On Error GoTo 0

End Sub

To call the procedure above the following code can be used within another procedure.  Change the paths to be the folder you wish to zip and the name you want the zip folder to be called.

Call CreateZipFile("C:\Users\marks\Documents\ZipThisFolder\", "C:\Users\marks\Documents\")

This procedure will overwrite any zip folder with the same name.


Unzip a zip file to a folder

Unzipping is a much easier process and only requires the files to be copied from the zip file into the folder.

Sub UnzipAFile(zippedFileFullName As Variant, unzipToPath As Variant)

Dim ShellApp As Object

'Copy the files & folders from the zip into a folder
Set ShellApp = CreateObject("Shell.Application")
ShellApp.Namespace(unzipToPath).CopyHere ShellApp.Namespace(zippedFileFullName).items

End Sub

To call the procedure above the following code can be used within another procedure.  Change the paths to be the name of the zip file you wish to unzip and the folder you wish to put the unzipped files into.

Call UnzipAFile("C:\Users\marks\Documents\", "C:\Users\marks\Documents\UnzipHereFolder\")


How does this code actually work?

It is rarely explained how this code creates a zip file.  Let me show you.

Create an empty zip file just using windows.  Right-click in a folder and select New-> Compressed (zipped) folder.

Create Zip folder with Windows

Now open that file in Notepad.  The section of code (highlighted in blue) informs windows this file is a zip file.  The file is empty, so there is no code from other files in there.

Open Zip folder in notepad

The code below is the line which inserts that same character string at the start of the file.  As a result, Windows believes this is a zip folder.

Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)

Even if we’ve created a zip file, we still need to get the files into it.  The native VBA code is not able to copy to/from a zip file, so the code uses the Shell.Application to copy the files.  Using the Shell.Application is similar to using the Windows environment, which is able to copy and paste files into a zip folder.

It is these two things together which really drive the functionality of this code.


17 thoughts on “VBA code to Zip and Unzip files and folders

    • Excel Off The Grid says:

      Hi Yasser,

      Possible causes for the ’91’ error could be:
      (1) The file name and file path to the zip file is not correct
      (2) The file path to the unzip location folder is not correct
      (3) The word ‘Set’ is missing from the start of the following line of code:
      Set ShellApp = CreateObject(“Shell.Application”)

      • Excel Off The Grid says:

        I should also say that whilst the code snippets above will create the Zip file, all the folder paths must already exist.

        There are other snippets in the code library to create, delete and check for existence of folder and files.

  1. Yasser says:

    Thank you very much for your reply
    I have my files on desktop >> the compressed file is “TestFolderZipped” and there is a folder named “TestFolder” on the desktop too
    And the excel file on the desktop too
    And I used this code

    Sub Test_UnZipFile()
    Dim strPath As String

    strPath = ThisWorkbook.Path & “\TestFolder\”
    If Len(Dir(strPath, vbDirectory)) = 0 Then MkDir strPath

    Call UnZipFile(ThisWorkbook.Path & “\”, strPath)
    MsgBox “Done…”, 64
    End Sub

    Sub UnZipFile(zippedFileFullName As Variant, unzipToPath As Variant)
    Dim shellApp As Object

    Set shellApp = CreateObject(“Shell.Application”)
    shellApp.Namespace(unzipToPath).CopyHere shellApp.Namespace(zippedFileFullName).items
    End Sub

    • Excel Off The Grid says:

      If you define the strPath as a Variant rather than a String, it should work.
      Replace this:
      Dim strPath As String

      With this:
      Dim strPath As Variant

      Let me know if that solves it.

  2. Yasser says:

    Thanks a lot .. That worked fine and great
    But why Variant
    Is this line : ThisWorkbook.Path & “\TestFolder\” is considered String?

    • Excel Off The Grid says:

      Somebody may correct me on this, but I believe it’s because the Shell.Application views it as a folder object rather than a String, which is why it needs to be a Variant. By passing a String between the procedures it remains as a String, therefore it is necessary to create it as a Variant initially.

  3. Ashwin says:

    Want small help from vba experts, have data with multiple clients mapped to employee in Excel which later I am saving in . PDF file format of every client mapped on the basis of employee for I. G.


    Now want to zip on the basis of emp and send email to emp

    Kindly suggest

    • Excel Off The Grid says:

      Hi Ashwin – as this is not a support forum, I can only help readers with specific problems if and when I have time.

      I have received your file, but not had chance to look at it yet. I hope to look at it later today.

      As it appears you want a faster resolution than I can provide, I suggest you try the Mr Excel forum. Alternatively Ron de Bruin’s site provides code you could adapt.

  4. Abou says:

    Dear All

    Hope you are fiine.

    I have an issue with my vba code. Would it be possible to help.

    Indeed, when I run the macro I have this issue: Argument not optional

    Many thanks for your support

    Sub Unzip()

    ‘Dim fpath1 As String

    Dim fpath, fpath2 As String
    Dim fname00, fname01 As Range

    Set fname00 = Range(“E1”)
    Set fname01 = Range(“E2”)

    ‘fpath1 = Worksheets(“DB”).Range(“E6”).Value

    fpath2 = Worksheets(“DB”).Range(“E10”).Value

    For i = 1 To 3

    fpath = “G:\DGI\Commun\Risk Management\04. Gestion des risques\16.Compliance Reports Zest\” & fname00 & “\” & fname01 & “\” & “Reports” & (i) & “.zip”

    Call UnzipAFile(fpath, fpath2)

    Next i

    End Sub

    Sub UnzipAFile(zippedFileFullName As Variant, unzipToPath As Variant)

    Dim ShellApp As Object

    ShellApp = CreateObject(“Shell.Application”)

    Shell.Namespace(unzipToPath).copyhere Shell.Namespace(zippedFileFullName).items

    End Sub

    • Excel Off The Grid says:

      Hi Abou,

      From looking at your code there appear to be a few of issues:

      1) fpath and fpath2 need to be created a Variant variables, rather than String variables. (See Yasser’s question above).

      2) ShellApp = CreateObject(“Shell.Application”) needs have Set at the start because it is an object. It becomes:
      Set ShellApp = Create….etc.

      3) Both instances of Shell.Namespace in the last line of code should be changed to ShellApp.Namespace

      Then I think it should work.

Leave a Reply

Your email address will not be published. Required fields are marked *