当我按下一张纸上的按钮时,我正在尝试将CSV文件导入另一张纸。
这是我的代码:
Sub historiskAktieData(ticker)
Set ws = ActiveWorkbook.Sheets("HistoriskAktieData")
Dim enddate As Date
Dim StartDate As Date
StartDate = ThisWorkbook.Sheets("Opg. 1").Range("c1").Value
enddate = ThisWorkbook.Sheets("Opg. 1").Range("c2").Value
Symbol = ticker
qurl = "http://ichart.finance.yahoo.com/table.csv?s=" & Symbol & "&a=" & Month(StartDate) - 1 & "&b=" & Day(StartDate) & "&c=" & Year(StartDate) & "&d=" & Month(enddate) - 1 & "&e=" & Day(enddate) & "&f=" & Year(enddate) & "&g=" & "" & "&q=q&y=0&z=" & Symbol & "&x=.csv"
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & qurl _
, Destination:=ws.Range("A1"))
.Name = "table.csv?s=SAP&a=01&b=01&c=14&d=01&e=08&f=16&g=Dateq=q&y=0&z=&x="
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
问题似乎是这一点:
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & qurl _
, Destination:=ws.Range("A1"))
希望有人可以提供帮助。我正在使用excel 2010。
答案 0 :(得分:0)
从 https://msdn.microsoft.com/en-us/library/office/ff837764.aspx
目的地,必填,范围
查询表目标范围左上角的单元格 (生成查询表的范围)。该 目标范围必须位于包含该目标的工作表上 由表达式指定的QueryTables对象。
您尝试创建的QueryTable位于活动工作表(ActiveSheet.QueryTables.Add(...))中,但Destination:= ws.Range(“A1”)通常不在同一工作表中。只需编写ws.QueryTables.Add(...)而不是ActiveSheet.QueryTables.Add(...)。