How to unzip a Zip file automatically using VBA. Watch the video below.
Here is the complete VBA code to unzip a Zip file:
Sub UnzipFile()
‘With this sample code you can browse to the zip file.
‘After we select the zip file the macro will create a new folder
‘and unzip the Zip file in that folder
Dim ShellApp As Object
Dim sDefPath As String
Dim sDate As String
Dim zippedFileFolderPath As Variant
Dim unzipToFolderPath As Variant
zippedFileFolderPath = Application.GetOpenFilename(filefilter:=”Zip Files (*.zip), *.zip”, _
MultiSelect:=False)
sDefPath = “C:\users\takyar\test-unzip\”
sDate = Format(Now, ” dd-mm-yy h-mm-ss”)
unzipToFolderPath = sDefPath & “MyUnzipFolder” & “-” & sDate & “\”
‘Make a new folder
MkDir unzipToFolderPath
‘Copy the files & folders from the zip into a folder with ShellApp
Set ShellApp = CreateObject(“Shell.Application”)
ShellApp.Namespace(unzipToFolderPath).CopyHere ShellApp.Namespace(zippedFileFolderPath).items
End Sub
Why we use the Application.GetOpenFilename method (Excel). We could also use the input-box but then we would need to remember the complete path to the file. The GetOpenFilename method displays the standard Open dialog box and gets a file name from the user without actually opening any files. Only when we select a file, the file is opened when we complete the dialog box. The syntax of GetOpenFilename is:
Syntax
expression.GetOpenFilename (FileFilter, FilterIndex, Title, ButtonText, MultiSelect)
The Expression variable represents an Application object.
Here is an explanation of the parameters used in the GetOpenFilename method:
Name | Required/Optional | Data type | Description |
---|---|---|---|
FileFilter | Optional | Variant | A string specifying file filtering criteria. |
FilterIndex | Optional | Variant | Specifies the index numbers of the default file filtering criteria, from 1 to the number of filters specified in FileFilter. If this argument is omitted or greater than the number of filters present, the first file filter is used. |
Title | Optional | Variant | Specifies the title of the dialog box. If this argument is omitted, the title is “Open.” |
ButtonText | Optional | Variant | Macintosh only |
MultiSelect | Optional | Variant | True to allow multiple file names to be selected. False to allow only one file name to be selected. The default value is False. |
Remarks
This string passed in the FileFilter argument consists of pairs of file filter strings followed by the MS-DOS wildcard file filter specification, with each part and each pair separated by commas. Each separate pair is listed in the Files of type drop-down list box. For example, the following string specifies two file filters—text and addin:
“Text Files (.txt),.txt, Add-In Files (.xla),.xla”
To use multiple MS-DOS wildcard expressions for a single file filter type, separate the wildcard expressions with semicolons; for example: “Visual Basic Files (*.bas; *.txt), .bas;.txt”.
If FileFilter is omitted, this argument defaults to “All Files (.),.“.
This method returns the selected file name or the name entered by the user. The returned name may include a path specification. If MultiSelect is True, the return value is an array of the selected file names (even if only one file name is selected). Returns False if the user cancels the dialog box.
This method may change the current drive or folder.

How to create a new directory with the MKDir command:
Sub CreateDirectory()
MkDir “c:\users\takyar\myexceltest”
End Sub
The above code will create a new directory in the subfolder ‘takyar’ called myexceltest.
We have learnt earlier how to use the shell command.
