VBA输出到Excel单元格

zlhcx6iw  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(160)

我正在构建一个工作表,并试图将机场的TAF(预测)输出到Excel中的单元格B7中。调试器一直标记请求。打开带有错误消息“URL不使用可识别的协议”的行。任何建议都是最受欢迎的。

Sub InitializeTAF()
    Dim url As String
    Dim request As New WinHttpRequest
    Dim response As String
    Dim ids As String
    Dim format As String
    
    ' Set the TAF data request parameters
    ids = "RKSO"
    format = "raw"
    
    ' Construct the request URL
    url = "https://www.aviationweather.gov/taf/data?ids=RKSO&format=raw&date=&submit=Get+TAF+data" & format & "&stationString=" & ids
    
    ' Send the request and get the response data
    request.Open "GET", url = "https://www.aviationweather.gov/taf/data?ids=RKSO&format=raw&date=&submit=Get+TAF+data", False
    request.Send
    response = request.ResponseText
    
    ' Update the TAF display with the new data
    ActiveSheet.Range("B7").Value = response
    
    ' Refresh the TAF display every 2 minutes
    Application.OnTime Now + TimeValue("00:02:00"), "InitializeTAF"
End Sub

我尝试在www.example.com行中使用url = http://request.open,但仍然出现同样的错误。前面提到的宏的预期结果是将TAF的行输出到活动工作表的单元格B7中。

v8wbuo2f

v8wbuo2f1#

你的代码没问题,只需要修改一行:

Sub InitializeTAF()
    Dim url As String
    Dim request As New WinHttpRequest
    Dim response As String
    Dim ids As String
    Dim format As String
    
    ' Set the TAF data request parameters
    ids = "RKSO"
    format = "raw"
    
    ' Construct the request URL
    url = "https://www.aviationweather.gov/taf/data?ids=RKSO&format=raw&date=&submit=Get+TAF+data" & format & "&stationString=" & ids
    
    ' Send the request and get the response data
    request.Open "GET", url, False
    request.Send
    response = request.ResponseText
    
    ' Update the TAF display with the new data
    ActiveSheet.Range("B7").Value = response
    
    ' Refresh the TAF display every 2 minutes
    Application.OnTime Now + TimeValue("00:02:00"), "InitializeTAF"
End Sub

由于代码每2分钟连续运行一次,因此最好将工作表作为参数传递,而不是使用ActiveSheet

Sub InitializeTAF(ws as Worksheet)
   ....
   ws.Range("B7").Value = response
   ....
End Sub

相关问题