kbay808
asked on
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
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
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
Where you want the data to be added at the end of each sheet ?
gowlfow
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
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
ASKER
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).
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
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
ASKER
Good point. I need to clear all previous data before importing.
ok then the macro will, each time activated:
1) Clear all previous data
2) Import new data
ok ?
gowflow
1) Clear all previous data
2) Import new data
ok ?
gowflow
ASKER
Perfect
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:
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
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
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
ASKER
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
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\
Screen-shots.docx
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
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
ASKER
The file name is "Daily-Report.xlsx". I attached a screen shot too.
Screen-shot.JPG
Screen-shot.JPG
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
Is your version on your pc trial or registered ?
gowflow
ASKER
I have a registered verson of WINZIP32.EXE.
ASKER
The program location is "C:\Program Files (x86)\WinZip\WINZIP32.EXE"
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
Will look for switches and advise you a solution just hang on with me.
gowflow
ASKER
Thanks
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
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
ASKER
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
yes the first item should look green and the second one black
It is more difficult as do not have winzip to test.
gowflow
It is more difficult as do not have winzip to test.
gowflow
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for keeping with it
gowflow