我能够使用链接“http://www.advfn.com/stock-market/NASDAQ/NVDA/financials?btn=annual_reports&mode=company_data”上的网络查询将财务报表导入Excel。我将导入过程记录到一个宏中并修补了一些vba代码以使其运行。现在,我想根据我在单元格A1中的股票代码刷新导入的财务报表。但是,我得到一个下标超出范围错误,我似乎无法弄清楚为什么。请查看以下代码,并尽可能提供任何见解。
Sub finstate()
'
' finstate Macro
'
'
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.advfn.com/stock-market/NASDAQ/" & Worksheets("Input").Range("A1").Value & "/financials?btn=annual_reports&mode=company_data" _
, Destination:=Range("B2"))
.Name = "financials?btn=annual_reports&mode=company_data"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = False
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingAll
.WebTables = "6"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
非常感谢你!
答案 0 :(得分:0)
我认为它可能是您的参考资料,看起来您正在从输入表中读取股票代码值但是尝试写入活动表。 除了这种可能性之外,它似乎对我很有效。
我为当前工作表添加了变量和测试。第一行也改变了一点。
sTicker = Range("A1").Value
If sTicker = "" Then
MsgBox "No value to look up"
Exit Sub
End If
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.advfn.com/stock-market/NASDAQ/" & sTicker & "/financials?btn=annual_reports&mode=company_data" _
, Destination:=Range("B2"))