Unzip Zip File

How to unzip a Zip file automatically using VBA. Watch the video below.

Unzip Zip File Automatically

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”, _
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:

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:

NameRequired/OptionalData typeDescription
FileFilterOptional VariantA string specifying file filtering criteria.
FilterIndexOptional VariantSpecifies 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.
TitleOptional VariantSpecifies the title of the dialog box. If this argument is omitted, the title is “Open.”
ButtonTextOptional VariantMacintosh only
MultiSelectOptional VariantTrue to allow multiple file names to be selected. False to allow only one file name to be selected. The default value is False.
Parameters of GetOpenFilename method

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.

GetOpenFilename method
GetOpenFilename method

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.

Unzip Zip File Automatically with VBA
Unzip Zip File Automatically with VBA

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.