Solved

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

Posted on 2014-07-22
22
521 Views
Last Modified: 2014-07-31
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
Comment
Question by:kbay808
  • 12
  • 10
22 Comments
 
LVL 29

Expert Comment

by:gowflow
ID: 40216457
what is the zip file here ? Daily-Report.xls or Report.xlsx

gowflow
0
 
LVL 29

Expert Comment

by:gowflow
ID: 40216462
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
 
LVL 29

Expert Comment

by:gowflow
ID: 40216811
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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:kbay808
ID: 40217162
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
 
LVL 29

Expert Comment

by:gowflow
ID: 40217175
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
 

Author Comment

by:kbay808
ID: 40217199
Good point.  I need to clear all previous data before importing.
0
 
LVL 29

Expert Comment

by:gowflow
ID: 40217365
ok then the macro will, each time activated:
1) Clear all previous data
2) Import new data

ok ?
gowflow
0
 

Author Comment

by:kbay808
ID: 40217412
Perfect
0
 
LVL 29

Expert Comment

by:gowflow
ID: 40218081
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
 

Author Comment

by:kbay808
ID: 40219029
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
 
LVL 29

Expert Comment

by:gowflow
ID: 40219054
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
 

Author Comment

by:kbay808
ID: 40219069
The file name is "Daily-Report.xlsx".  I attached a screen shot too.
Screen-shot.JPG
0
 
LVL 29

Expert Comment

by:gowflow
ID: 40219111
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
 

Author Comment

by:kbay808
ID: 40219625
I have a registered verson of WINZIP32.EXE.
0
 

Author Comment

by:kbay808
ID: 40219630
The program location is "C:\Program Files (x86)\WinZip\WINZIP32.EXE"
0
 
LVL 29

Expert Comment

by:gowflow
ID: 40220153
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
 

Author Comment

by:kbay808
ID: 40220302
Thanks
0
 
LVL 29

Expert Comment

by:gowflow
ID: 40220471
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
 

Author Comment

by:kbay808
ID: 40220694
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
 
LVL 29

Expert Comment

by:gowflow
ID: 40221059
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
 
LVL 29

Accepted Solution

by:
gowflow earned 500 total points
ID: 40221061
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
 

Author Closing Comment

by:kbay808
ID: 40232439
Thanks for keeping with it
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question