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文件。让我看看我可以如何帮助您最后的评论。 –