Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 650
  • Last Modified:

How to create a macro to import data from a zip file into an existing spreadsheet?

Zip file location: The same directory as the file that contains this macro when ran
Zip file name: “Daily_Report.zip” (Overwrite existing file)
Unzip destination: Current directory
File name: “Daily_Report.xlsx”

Import data
From sheet: “New”
Import to: Current workbook
Destination sheet: “New”

From sheet: “Old”
Import to: Current workbook
Destination sheet: “Old”

Note: Unable to upload a zip fie so I just updated the sample file
Daily-Report.xlsx
Report.xlsx
0
kbay808
Asked:
kbay808
  • 12
  • 10
1 Solution
 
gowflowCommented:
what is the zip file here ? Daily-Report.xls or Report.xlsx

gowflow
0
 
gowflowCommented:
I presume Daily-Report.xlsx is the zipped file but your posted Report.xlsx has no data is that an error or it should be like this ?
Where you want the data to be added at the end of each sheet ?
gowlfow
0
 
gowflowCommented:
1 more still not clear where do you need to data to be saved to.

I understand that Daily-Report.xlsx is inside the Daily-Report.zip file and you need to read from Daily-Report.xlsx and save data where to Report.xlsx ??? or ... ???

pls clarify as I have the macro ready but just missing what to copy and to where.
gowflow
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kbay808Author Commented:
I need the macro in the Report.xlsx to pull the data from the Daily_Report.xlsx(zip) and populate the 2 cleared tabs (New & Old).
0
 
gowflowCommented:
ok let me get this right.

Macro is in Report.xlsm (as macro enabled workbook are xlsm and not xslx) the 2 tabs are empty and we copy over the data that exist from the zip file onto these 2 tabs.

Right ??

ok my question then is what happens next ??? the next day you have in Report.xlsm the data that we imported previously how to import the new data ???

You are missing something in your logic.
gowflow
0
 
kbay808Author Commented:
Good point.  I need to clear all previous data before importing.
0
 
gowflowCommented:
ok then the macro will, each time activated:
1) Clear all previous data
2) Import new data

ok ?
gowflow
0
 
kbay808Author Commented:
Perfect
0
 
gowflowCommented:
ok here is your solution.

This macro requires 7Zip that could be downloaded at this location as it works best with. Do not know if WinZip would give same results as tested it on WinRAR and it didn't perform correctly.

It requires 7-Zip (free / open source):
http://www.7-zip.org/


Here is the code for this macro:

Sub ImportDailyData()
Dim strFileName As String, str7ZIP As String, strZipFile As String, strDestinationFolder As String, strCMD As String
Dim WshShell As Object, fso As Object
Dim WB As Workbook
Dim ThisWB As Workbook
Dim WS As Worksheet
Dim ThisWS As Worksheet

strFileName = "Daily-Report.xlsx"
str7ZIP = "C:\Program Files\7-Zip\7z.exe"
strDestinationFolder = ActiveWorkbook.Path
strZipFile = strDestinationFolder & "\Daily-Report.zip"
Set ThisWB = ActiveWorkbook

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

Set WshShell = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

If Not fso.FileExists(str7ZIP) Then
    MsgBox "Could not find 7-Zip:  " & vbCrLf & vbCrLf & str7ZIP, vbExclamation, "Daily-Report.xlsx"
    Exit Sub
End If

strCMD = Chr(34) & str7ZIP & Chr(34) & " e -i!" & _
        Chr(34) & strFileName & Chr(34) & " -o" & _
        Chr(34) & strDestinationFolder & Chr(34) & " " & _
        Chr(34) & strZipFile & Chr(34) & " -y"


WshShell.Run strCMD, 0, True

If Not fso.FileExists(strDestinationFolder & strFileName) Then
    MsgBox "Failed to get file:  " & strDestinationFolder & strFileName, vbExclamation, "Daily-Report.xlsx"
    Exit Sub
Else
    '---> Stop Events
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
    
    '---> Open the Import Workbook
    Set WB = Workbooks.Open(strDestinationFolder & strFileName)
    
    '---> Clean Current Data in present workbook
    For Each WS In ThisWB.Worksheets
        If WS.Name <> "Main" Then
            WS.UsedRange.EntireRow.Delete
        End If
    Next WS
    '---> Get Data
    For Each WS In WB.Worksheets
        Set ThisWS = ThisWB.Worksheets(WS.Name)
        WS.UsedRange.Copy ThisWS.Range("A1")
        ThisWS.UsedRange.EntireColumn.AutoFit
    Next WS
    
    '---> Close WB
    Application.DisplayAlerts = False
    WB.Close savechanges:=False
    Kill strDestinationFolder & strFileName
    Application.DisplayAlerts = True
    
    '---> Clean Variables
    Set WB = Nothing
    Set WS = Nothing
    
    '---> Enable Events
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
    
    '---> Advise user
    MsgBox ("Import Daily data successfull.")
End If


End Sub

Open in new window



As you can see you should replace in this code (if you don't use 7Zip)
this line
str7ZIP = "C:\Program Files\7-Zip\7z.exe"

by the location of the Win Zip or Zip program on your pc

You will notice in the attach file that I added the sheet Main that holds a button that once you enable macros simply activate the button and it will import the data that is in the file: "Daily-Report.zip" in the New and Old Tabs in this workbook.

Let me know your comments.
gowflow
Report.xlsm
0
 
kbay808Author Commented:
This is being used on my company’s computer and I’m unable to install new software.  We only have WinZip.  Unfortunately the scrip does not work.  All it did was make a copy of the zip file.  I attached a word document with all of the screen shots of the process when I clicked on the button that you created.  I also added notes.

Here is the change that I made to the code
str7ZIP = "C:\Program Files (x86)\WinZip\WINZIP32.exe"  

Additional info:
Name of the folder that contains the zip file and the report file: “Report”
Folder Path: "C:\%userprofile%\desktop\Report"
Screen-shots.docx
0
 
gowflowCommented:
ok noted

try this doubleclick manually on the zip file do you see the file Daily-Report.xlsx in there ? it should be named exactly like this as it is hardcoded in the macro.

I will try to look for a copy of WinZip32 and make some tests.

Pls let me know first answer to my question.
gowlfow
0
 
kbay808Author Commented:
The file name is "Daily-Report.xlsx".  I attached a screen shot too.
Screen-shot.JPG
0
 
gowflowCommented:
ok I tried downloading WINZIP64.exe but seems it does not work as need a registered version.

Is your version on your pc trial or registered ?
gowflow
0
 
kbay808Author Commented:
I have a registered verson of WINZIP32.EXE.
0
 
kbay808Author Commented:
The program location is "C:\Program Files (x86)\WinZip\WINZIP32.EXE"
0
 
gowflowCommented:
Yes I know problem is I cannot test it as do not have the pgm and to hv a registered version u need to buy it !!! :)
Will look for switches and advise you a solution just hang on with me.
gowflow
0
 
kbay808Author Commented:
Thanks
0
 
gowflowCommented:
Lets try the following:

Comment out this instruction (put a single quote in the first line so it look like this:

'strCMD = Chr(34) & str7ZIP & Chr(34) & " e -i!" & _
        Chr(34) & strFileName & Chr(34) & " -o" & _
        Chr(34) & strDestinationFolder & Chr(34) & " " & _
        Chr(34) & strZipFile & Chr(34) & " -y"

and add this line just after it

strCMD = Chr(34) & str7ZIP & Chr(34) & " -min -e " & _
        Chr(34) & strFileName & Chr(34) & " -o" & _
        Chr(34) & strDestinationFolder & Chr(34) & " " & _
        Chr(34) & strZipFile & Chr(34) & " -y"

save the macro and try running it
Let me know
gowflow
0
 
kbay808Author Commented:
Should it look like this?  I tried it without the ' before "'strCMD = Chr(34) & str7ZIP & Chr(34) & " e -i!" & _" too.  Both ways did nothing.

'Comment out this instruction (put a single quote in the first line so it look like this:

 'strCMD = Chr(34) & str7ZIP & Chr(34) & " e -i!" & _
         Chr(34) & strFileName & Chr(34) & " -o" & _
         Chr(34) & strDestinationFolder & Chr(34) & " " & _
         Chr(34) & strZipFile & Chr(34) & " -y"

 strCMD = Chr(34) & str7ZIP & Chr(34) & " -min -e " & _
         Chr(34) & strFileName & Chr(34) & " -o" & _
         Chr(34) & strDestinationFolder & Chr(34) & " " & _
         Chr(34) & strZipFile & Chr(34) & " -y"

End Sub

Open in new window

0
 
gowflowCommented:
yes the first item should look green and the second one black
It is more difficult as do not have winzip to test.
gowflow
0
 
gowflowCommented:
ok lets try this one:
Include this code

strCMD = Chr(34) & str7ZIP & Chr(34) & " -min -e " & _
        Chr(34) & strZipFile & Chr(34) & _
        Chr(34) & strDestinationFolder & Chr(34)

Open in new window


instead of this code

strCMD = Chr(34) & str7ZIP & Chr(34) & " -min -e " & _
         Chr(34) & strFileName & Chr(34) & " -o" & _
         Chr(34) & strDestinationFolder & Chr(34) & " " & _
         Chr(34) & strZipFile & Chr(34) & " -y"

Open in new window


Let me know
gowflow
0
 
kbay808Author Commented:
Thanks for keeping with it
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 12
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now