此Excel for Mac代码从Yahoo下载库存数据表并将其插入标有“DataSheet”的工作表中。它从工作表“QuerySheet”获取自动收报机,开始和结束日期。
此代码在QueryTable对象的.Refresh方法上失败,我不明白为什么。什么是正确的语法?
Sub data_get()
Dim ticker As String, sday, smonth, syear, eday, emonth, eyear As Long
Dim QuerySheet As Worksheet
Dim DataSheet As Worksheet
Set QuerySheet = ActiveWorkbook.Sheets("QuerySheet")
Set DataSheet = ActiveWorkbook.Sheets("DataSheet")
DataSheet.Columns("A:G").ClearContents
With QuerySheet
ticker = Range("A2")
sday = Day(Range("G2"))
smonth = Month(Range("G2"))
syear = Year(Range("G2"))
eday = Day(Range("H2"))
emonth = Month(Range("H2"))
eyear = Year(Range("H2"))
End With
With DataSheet.QueryTables.Add(Connection:= _
"TEXT;http://real-chart.finance.yahoo.com/table.csv?s=" & ticker & "&d=" & emonth & "&e=" & eday & "&f=" & eyear & "&g=d&a=" & smonth & "&b=" & sday & "&c=" & syear & "&ignore=.csv" _
, Destination:=DataSheet.Range("$A$1"))
.Name = "table1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
'.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
'.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
'.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(5, 1, 1, 1, 1, 1, 1)
'.TextFileTrailingMinusNumbers = True
'.BackgroundQuery = True
.Refresh BackgroundQuery:=False <---- 1004 Error
'.Refresh BackgroundQuery:=True
End With
End Sub