Zipping Files

luckee

New Member
Joined
Sep 23, 2022
Messages
25
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.
 
Did you check for illegal characters, as I suggested in my previous post?
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Did you check for illegal characters, as I suggested in my previous post?
One more question - the completed zip file is saved to My Documents. is there a way to have it save in the same path as where the files being zipped were?
 
Upvote 0
Try the following...

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

If Right(FolderName, 1) <> "\" Then
    FolderName = FolderName & "\"
End If

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

Notice that this time we're making sure that the string assigned to FolderName ends in a backslash ( \ ), since it's now being used as the path for the zipped file.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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