Arrays JSON导入生成运行时错误';13';类型不匹配
尝试使用JSON通过IEX API导入某些数据时,我收到运行时错误“13”类型不匹配 我在为for Each循环中的单元格设置值时收到错误 以下是查看API数据的链接:Arrays JSON导入生成运行时错误';13';类型不匹配,arrays,json,excel,vba,type-mismatch,Arrays,Json,Excel,Vba,Type Mismatch,尝试使用JSON通过IEX API导入某些数据时,我收到运行时错误“13”类型不匹配 我在为for Each循环中的单元格设置值时收到错误 以下是查看API数据的链接: Sub-getFinancials() '写入ws 将ws设置为工作表 Set ws=资产负债表(“财务”) 作为字符串的Dim-ticker ticker=ws.范围(“P7”)值 最后一排一样长 lastrow=ws.Cells(Rows.Count,“A”).End(xlUp).Row “净射程 ws.Range(“A1
Sub-getFinancials()
'写入ws
将ws设置为工作表
Set ws=资产负债表(“财务”)
作为字符串的Dim-ticker
ticker=ws.范围(“P7”)值
最后一排一样长
lastrow=ws.Cells(Rows.Count,“A”).End(xlUp).Row
“净射程
ws.Range(“A1:L”和lastrow)。清除
'数组列标题
Dim myarray作为变体
myarray=数组(“报告日期”、“利润总额”、“收益成本”、“运营收入”、“总收入”、“运营收入”、“净收入”、“研发”、“运营费用”、“流动资产”、“总资产”、“总负债”、“总现金”、“总债务”、“股东权益”、“现金变动”、“现金流量”,“经营性损益”)
Arrsize=UBound(myarray)-LBound(myarray)+1
变光目标As范围
设置rngTarget=ws.Range(单元格(2,1),单元格(Arrsize+1,1))
rngTarget.value=Application.Transpose(myarray)
'发送API数据的web请求
u=”https://api.iextrading.com/1.0/stock/“&ticker&/financialsperiod=年度”
' https://api.iextrading.com/1.0/stock/aapl/financials?period=annual
设置myrequest=CreateObject(“WinHttp.WinHttpRequest.5.1”)
我的请求。打开“获取”,u
我的请求。发送
'解析JSON
Dim JSON作为对象
Set JSON=JsonConverter.ParseJson(myrequest.ResponseText)
数组中对象的“Get”
Dim arrayLen作为整数
arrayLen=JSON.Count
'通过元素循环
作为变体的Dim元素
尺寸x、y、r为整数
r=2
y=2
x=1
而x
我刚刚通过转换器运行了JSON,这是我得到的结构:
- -字典(2项)
- --收藏(4项)
- ---字典(20项)
Option Explicit
Sub PrintFinancialReports()
Dim apiURL As String
apiURL = "https://api.iextrading.com/1.0/stock/aapl/financials?period=annual"
Dim myrequest As WinHttpRequest
Set myrequest = New WinHttpRequest
myrequest.Open "Get", apiURL
myrequest.Send
Debug.Print myrequest.ResponseText ' print received JSON to check if it is valid
Dim FinancialReportQuery As Dictionary
Set FinancialReportQuery = JsonConverter.ParseJson(myrequest.ResponseText)
Debug.Print FinancialReportQuery.Item("symbol")
Dim Reports As Collection
Set Reports = FinancialReportQuery.Item("financials")
Dim report As Dictionary
For Each report In Reports
Dim reportContentKey As Variant '<-- variant is needed to loop a dictionary
For Each reportContentKey In report
Debug.Print reportContentKey, report.Item(reportContentKey)
Next reportContentKey
Next report
End Sub
选项显式
次级打印财务报告()
将URL设置为字符串
apiURL=”https://api.iextrading.com/1.0/stock/aapl/financials?period=annual"
将myrequest设置为WinHttpRequest
设置myrequest=New WinHttpRequest
myrequest.Open“Get”,apirl
我的请求。发送
Debug.Print myrequest.ResponseText'打印收到的JSON以检查其是否有效
Dim Financial ReportQuery作为字典
Set FinancialReportQuery=JsonConverter.ParseJson(myrequest.ResponseText)
调试.打印FinancialReportQuery.Item(“符号”)
作为集合的Dim报告
Set Reports=FinancialReportQuery.Item(“financials”)
作为字典的模糊报告
对于报告中的每个报告
Dim reportContentKey作为变量“请注意JSON(2)(元素)应该是JSON(x)(元素).H A Kapadia.欢迎来到stackoverflow!。供您参考:Dim x,y,r As Integer
将为您提供x As Object
,y As Object
和r As Integer
。您需要键入的是Dim x As Integer,y As Integer,r As Integer
@Ahmad,实际上x和y将是变量。尽管反求更好clare x和y显式表示为整数,如果其他所有内容都正确,则此声明不会阻止它工作。JSONConverter.ParseJson可以返回集合或字典(因此您的arrayLen Variabel已经令人困惑)。请尝试使用“监视”窗口查看数据的结构。JSON(2)(元素)中的双大括号看起来也有问题……如果“JSON”是一个字典,那么您正试图将“element”中的任何内容写入键“2”(整数/长),尽管它需要一个字符串。您希望JSON对象提供什么数据。
Option Explicit
Sub PrintFinancialReports()
Dim apiURL As String
apiURL = "https://api.iextrading.com/1.0/stock/aapl/financials?period=annual"
Dim myrequest As WinHttpRequest
Set myrequest = New WinHttpRequest
myrequest.Open "Get", apiURL
myrequest.Send
Debug.Print myrequest.ResponseText ' print received JSON to check if it is valid
Dim FinancialReportQuery As Dictionary
Set FinancialReportQuery = JsonConverter.ParseJson(myrequest.ResponseText)
Debug.Print FinancialReportQuery.Item("symbol")
Dim Reports As Collection
Set Reports = FinancialReportQuery.Item("financials")
Dim report As Dictionary
For Each report In Reports
Dim reportContentKey As Variant '<-- variant is needed to loop a dictionary
For Each reportContentKey In report
Debug.Print reportContentKey, report.Item(reportContentKey)
Next reportContentKey
Next report
End Sub