Regex 运行时错误424:对象要求-使用API链接提取数据
我正在使用VBA代码使用API链接从网站中提取数据 我试图使用正则表达式将Regex 运行时错误424:对象要求-使用API链接提取数据,regex,vba,web-scraping,Regex,Vba,Web Scraping,我正在使用VBA代码使用API链接从网站中提取数据 我试图使用正则表达式将usd\U sgd和jpy\U sgd\U 100的汇率部分提取到单元格B2和C2中的同一工作表中 我尝试使用if-else语句来表示两种不同的模式“usd\U sgd”和“jpy\U sgd\U 100” 当我运行代码时,它会产生一个运行时错误: “运行时错误424需要对象” 在线: For Each match In matches 我想将“美元新元”和“日元新元100”的汇率数据从规定的API链接提取到同一工作表的
usd\U sgd
和jpy\U sgd\U 100
的汇率部分提取到单元格B2和C2中的同一工作表中
我尝试使用if-else
语句来表示两种不同的模式“usd\U sgd”
和“jpy\U sgd\U 100”
当我运行代码时,它会产生一个运行时错误:
“运行时错误424需要对象”
在线:
For Each match In matches
我想将“美元新元”和“日元新元100”的汇率数据从规定的API链接提取到同一工作表的单元格B2和C2中。我如何解决当前错误并实现此结果
代码:
您从未设置过
.Pattern
,因此您从未设置过.Execute
,因此,您从未将匹配
设置为除无
之外的任何内容。将.pattern
设置为所需的模式
重新写入可能如下所示:
Public Sub ExchangeRate()
Dim results(), s As String
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://eservices.mas.gov.sg/api/action/datastore/search.json?resource_id=5aa64bc2-d234-43f3-892e-2f587a220f74&fields=end_of_week,usd_sgd,jpy_sgd_100&limit=1&sort=end_of_week%20desc", False
.send
s = .responseText
End With
Dim pattern As Variant, patterns(), i As Long
patterns = Array("""usd_sgd"":""(.*?)""", """jpy_sgd_100"":""(.*?)""")
ReDim results(1 To UBound(patterns) + 1)
With CreateObject("VBScript.RegExp")
.Global = True
.IgnoreCase = False
.MultiLine = True
For i = LBound(patterns) To UBound(patterns)
.pattern = patterns(i)
Set matches = .Execute(s)
If matches.Count > 0 Then results(i + 1) = matches(0).Submatches(0)
Next
End With
With ThisWorkbook.Worksheets("Sheet1")
.Cells(2, 2).Resize(UBound(results), 1) = Application.Transpose(results)
End With
End Sub
Public Sub ExchangeRate()
Dim results(), s As String
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://eservices.mas.gov.sg/api/action/datastore/search.json?resource_id=5aa64bc2-d234-43f3-892e-2f587a220f74&fields=end_of_week,usd_sgd,jpy_sgd_100&limit=1&sort=end_of_week%20desc", False
.send
s = .responseText
End With
Dim pattern As Variant, patterns(), i As Long
patterns = Array("""usd_sgd"":""(.*?)""", """jpy_sgd_100"":""(.*?)""")
ReDim results(1 To UBound(patterns) + 1)
With CreateObject("VBScript.RegExp")
.Global = True
.IgnoreCase = False
.MultiLine = True
For i = LBound(patterns) To UBound(patterns)
.pattern = patterns(i)
Set matches = .Execute(s)
If matches.Count > 0 Then results(i + 1) = matches(0).Submatches(0)
Next
End With
With ThisWorkbook.Worksheets("Sheet1")
.Cells(2, 2).Resize(UBound(results), 1) = Application.Transpose(results)
End With
End Sub