0
我偶然發現了這段代碼,但我很難讓它工作。我試圖從網站下載一個包含.csv的zip文件,並將這些內容放入我的excel文件中。我目前被卡在這一行:從網上下載Zip文件(包含.csv)到excel VBA
'3 rename file
Name targetFileCSV As targetFileTXT
它說它找不到文件。
任何幫助表示讚賞!
'Main Procedure
Sub LETSDOTHIS()
Dim url As String
Dim targetFolder As String, targetFileZip As String, targetFileCSV As String, targetFileTXT As String
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim sDelimiter As String
Dim newSheet As Worksheet
url = "http://www20.statcan.gc.ca/tables-tableaux/cansim/csv/00260008-eng.zip"
targetFolder = Environ("TEMP") & "\" & RandomString(6) & "\"
MkDir targetFolder
targetFileZip = targetFolder & "data.zip"
targetFileCSV = targetFolder & "data.csv"
targetFileTXT = targetFolder & "data.txt"
'1 download file
DownloadFile url, targetFileZip
'2 extract contents
Call UnZip(targetFileZip, targetFolder)
'3 rename file
Name targetFileCSV As targetFileTXT
'4 Load data
Call LoadFile(targetFileTXT)
End Sub
Private Sub DownloadFile(myURL As String, target As String)
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.send
myURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile target, 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If
End Sub
Private Function RandomString(cb As Integer) As String
Randomize
Dim rgch As String
rgch = "abcdefghijklmnopqrstuvwxyz"
rgch = rgch & UCase(rgch) & ""
Dim i As Long
For i = 1 To cb
RandomString = RandomString & Mid$(rgch, Int(Rnd() * Len(rgch) + 1), 1)
Next
End Function
Private Function UnZip(PathToUnzipFileTo As Variant, FileNameToUnzip As Variant)
' Unzips a file
' Note that the default OverWriteExisting is true unless otherwise specified as False.
Dim objOApp As Object
Dim varFileNameFolder As Variant
varFileNameFolder = PathToUnzipFileTo
Set objOApp = CreateObject("Shell.Application")
' the "24" argument below will supress any dialogs if the file already exist. The file will
' be replaced. See http://msdn.microsoft.com/en-us/library/windows/desktop/bb787866(v=vs.85).aspx
'objOApp.Namespace(FileNameToUnzip).CopyHere objOApp.Namespace(varFileNameFolder).items, 24
' Call UnZip(targetFolder, targetFileZip)
End Function
Private Sub UnZips(mainFolder As Variant, zipFolder As Variant)
Call UnZip(targetFolder, targetFileZip)
End Sub
Private Sub LoadFile(file As String)
Set wkbTemp = Workbooks.Open(Filename:=file, Format:=xlCSV, Delimiter:=";", ReadOnly:=True)
wkbTemp.Sheets(1).Cells.Copy
'here you just want to create a new sheet and paste it to that sheet
Set newSheet = ThisWorkbook.Sheets.Add
With newSheet
.Name = wkbTemp.Name
.PasteSpecial
End With
Application.CutCopyMode = False
wkbTemp.Close
End Sub
我是否需要將「data」替換爲「00260008-eng.csv」。 targetFileZip = targetFolder& 「00260008-eng.csv.zip」 targetFileCSV = targetFolder& 「00260008-eng.csv.csv」 targetFileTXT = targetFolder& 「00260008-eng.csv.txt」 我嘗試這樣做,沒有按似乎沒有用。我如何去做這件事?謝謝 – RageAgainstheMachine
我該如何做到這一點:「您需要重新命名解壓縮的文件,或者在解壓縮後查找沒有.zip的文件。」謝謝 – RageAgainstheMachine
@RageAgainstheMachine我只用'targetFileCSV = targetFolder&「00260008-eng.csv」'替換了'targetFileCSV = targetFolder&「data.csv」',它對我很有幫助。但是,我不會依賴這種方法,因爲每次下載都會改變文件的名稱。此外,您不必從'.csv'重命名爲'.txt'以在Excel中打開文件。 Excel接受CSV文件。讓我看看我可以如何幫助您最後的評論。 –