Help needed for zip files macro

avidlearner

New Member
Joined
Apr 12, 2009
Messages
5
Hi all,

I'm new to this forum and need help with a zip file macro. Basically I need the macro to zip up a number of files separately. I have something like this:

Sub Zipfile1()
Dim source As String
Dim target As String

source = "F:\Macro test\LNSPY.xls"
target = "F:\Macro test\LNSPY.zip"

Shell "C:\Program Files\WinZip\WINZIP32.EXE -a -r " & target & " " & source
End Sub

However, when i run the macro, there's a error saying "No files were found for this operation - nothing to do. (F:\Macro.zip).

Can someone help by telling me what is missing in my macro? Also, can you show me how to create a loop so that after zipping file1 = LNSPY.xls, I can zip up 3 more other files with a different name.

Thanks in advance.
 

Some videos you may like

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Diablo II

Well-known Member
Joined
Sep 28, 2008
Messages
538
try like this
Code:
ZipPath = "c:\programme\winzip\winzip32.exe"
  Shell ZipPath & " -a " & target & source & ".zip " & _
    target & source & ".xls"
  Application.Wait Now + TimeSerial(0, 0, 2)
 

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209
Hi all,

I'm new to this forum and need help with a zip file macro. Basically I need the macro to zip up a number of files separately. I have something like this:

Sub Zipfile1()
Dim source As String
Dim target As String

source = "F:\Macro test\LNSPY.xls"
target = "F:\Macro test\LNSPY.zip"

Shell "C:\Program Files\WinZip\WINZIP32.EXE -a -r " & target & " " & source
End Sub

However, when i run the macro, there's a error saying "No files were found for this operation - nothing to do. (F:\Macro.zip).

Can someone help by telling me what is missing in my macro? Also, can you show me how to create a loop so that after zipping file1 = LNSPY.xls, I can zip up 3 more other files with a different name.

Thanks in advance.

Shell interprets spaces as command sepeartors, you need to enclose them in "

one rougth way

Code:
    If InStr(1, source, " ", vbTextCompare) <> 0 Then
        source = Chr$(34) & source & Chr$(34)
    End If

    If InStr(1, target, " ", vbTextCompare) <> 0 Then
        target = Chr$(34) & target & Chr$(34)
    End If

As far as your 2nd Q.
Build up an Array and loop through this replacing your source and target strings with the new strings.
 

avidlearner

New Member
Joined
Apr 12, 2009
Messages
5
Hi all,

Thanks for your reply!

Ivan,

I did this and it works! :)

Sub Zipfile1()
Dim source As String
Dim target As String
source = "F:\Macro test\LNSPY.xls"
target = "F:\Macro test\LNSPY.zip"
If InStr(1, source, " ", vbTextCompare) <> 0 Then
source = Chr$(34) & source & Chr$(34)
End If
If InStr(1, target, " ", vbTextCompare) <> 0 Then
target = Chr$(34) & target & Chr$(34)
End If
Shell "C:\Program Files\WinZip\WINZIP32.EXE -a -r " & target & " " & source
End Sub

But I don't really understand or know how to write a loop command. Can you show me how to create a loop so that i can zip for e.g first file - "F:\Macro test\LNSPY.xls" then "F:\Macro test\clean\split.xls"? The loop will zip up all the files i define.

Once again, thanks in advance ;)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,334
Messages
5,595,567
Members
413,996
Latest member
mabelO

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
Top