Zipping Files

luckee

New Member
Joined
Sep 23, 2022
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
Hi, want to make some changes to the below in red so it picks up the information from the active sheet rather than changing in the code.


Sub Zip_All_Files_in_Folder()
Dim FileNameZip, FolderName
Dim strDate As String, DefPath As String
Dim oApp As Object

DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If

FolderName = "C:\Users\Ron\test\" '<< Change 1. How to change this so it can reference the path on Sheet1 cell A1?

strDate = Format(Now, " dd-mmm-yy h-mm-ss")
FileNameZip = DefPath & "MyFilesZip " & strDate & ".zip" 2. How to change this so it can pick up the file name from Sheet1 cell A2?

'Create empty Zip File
NewZip (FileNameZip)

Set oApp = CreateObject("Shell.Application")
'Copy the files to the compressed folder
oApp.Namespace(FileNameZip).CopyHere oApp.Namespace(FolderName).items

'Keep script waiting until Compressing is done
On Error Resume Next
Do Until oApp.Namespace(FileNameZip).items.Count = _
oApp.Namespace(FolderName).items.Count
Application.Wait (Now + TimeValue("0:00:01"))
Loop
On Error GoTo 0

MsgBox "You find the zipfile here: " & FileNameZip
End Sub

Thank you.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try...

VBA Code:
FolderName = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value

and

VBA Code:
FileNameZip = DefPath & ThisWorkbook.Worksheets("Sheet1").Range("A2").Value

Change the workbook and worksheet references as desired.

Hope this helps!
 
Upvote 0
Solution
Try...

VBA Code:
FolderName = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value

and

VBA Code:
FileNameZip = DefPath & ThisWorkbook.Worksheets("Sheet1").Range("A2").Value

Change the workbook and worksheet references as desired.

Hope this helps!
Hi, thank you! The FolderName worked but I'm getting a Run-time error '76': Path not found message for the file name
 
Upvote 0
In that case, try the following instead...

VBA Code:
FileNameZip = DefPath & ThisWorkbook.Worksheets("Sheet1").Range("A2").Value & ".zip"
 
Upvote 0
In that case, try the following instead...

VBA Code:
FileNameZip = DefPath & ThisWorkbook.Worksheets("Sheet1").Range("A2").Value & ".zip"
Hi, still getting the same error...the below in red highlights in yellow. Thank you.

Sub NewZip(sPath)
'Create empty Zip File
'Changed by keepITcool Dec-12-2005
If Len(Dir(sPath)) > 0 Then Kill sPath
Open sPath For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
End Sub
 
Upvote 0
Hi, still getting the same error...the below in red highlights in yellow. Thank you.

Sub NewZip(sPath)
'Create empty Zip File
'Changed by keepITcool Dec-12-2005
If Len(Dir(sPath)) > 0 Then Kill sPath
Open sPath For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
End Sub
Putting the entire code...getting Run-time error '13': Type mismatch

Sub NewZip(sPath)
'Create empty Zip File
'Changed by keepITcool Dec-12-2005
If Len(Dir(sPath)) > 0 Then Kill sPath
Open sPath For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
End Sub


Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function


Function Split97(sStr As Variant, sdelim As String) As Variant
'Tom Ogilvy
Split97 = Evaluate("{""" & _
Application.Substitute(sStr, sdelim, """,""") & """}")
End Function


Sub Zip_All_Files_in_Folder()
Dim FileNameZip, FolderName
Dim strDate As String, DefPath As String
Dim oApp As Object

DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If

FolderName = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value '<< Change

FileNameZip = DefPath & ThisWorkbook.Worksheets("Sheet1").Range("A2").Value & ".zip"

'Create empty Zip File
NewZip (FileNameZip)

Set oApp = CreateObject("Shell.Application")
'Copy the files to the compressed folder
oApp.Namespace(FileNameZip).CopyHere oApp.Namespace(FolderName).items

'Keep script waiting until Compressing is done
On Error Resume Next
Do Until oApp.Namespace(FileNameZip).items.Count = _
oApp.Namespace(FolderName).items.Count
Application.Wait (Now + TimeValue("0:00:01"))
Loop
On Error GoTo 0

MsgBox "You find the zipfile here: " & FileNameZip
End Sub
 
Upvote 0
If Len(Dir(sPath)) > 0 Then Kill sPath
I don't see how you're getting a type mismatch error for the above line of code. In any case, try running your code line-by-line by pressing F8 until you've executed this line...

VBA Code:
FileNameZip = DefPath & ThisWorkbook.Worksheets("Sheet1").Range("A2").Value & ".zip"

Then enter the following in the Immediate Window (Ctrl-G), and press the ENTER key...

VBA Code:
? FileNameZip

Now, make sure that the returned string contains a valid path, and that the filename doesn't contain any illegal characters.
 
Upvote 0
I don't see how you're getting a type mismatch error for the above line of code. In any case, try running your code line-by-line by pressing F8 until you've executed this line...

VBA Code:
FileNameZip = DefPath & ThisWorkbook.Worksheets("Sheet1").Range("A2").Value & ".zip"

Then enter the following in the Immediate Window (Ctrl-G), and press the ENTER key...

VBA Code:
? FileNameZip

Now, make sure that the returned string contains a valid path, and that the filename doesn't contain any illegal characters.
Thank you. Valid path, no issues...I get a Run-time error '76': Path not found on this line now - Open sPath For Output As #1
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,101
Members
449,096
Latest member
provoking

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top