使用QuertyTables.Add VBA的动态URL

时间:2017-08-30 15:55:10

标签: vba url dynamic

我正在尝试从天气网站(weather.gov)下载数据并尝试在URL中使用纬度和经度变量。如果我去网站并手动切换纬度和经度,我将被引导到正确的城市天气预报。但是,当我尝试将纬度和经度设置为变量并将未更改的URL部分与每个城市的变量组合时,它会给我一个编译错误Expected: list separator or )。它一直停留在URL中的("&lon=-")部分。我不确定是否有更好的方法来声明变量或添加它们,但它对我来说没有多大意义,为什么它不喜欢那个中间部分。代码如下。谢谢!

P.S。 cityLatcityLong变量将是基于城市的值,但是现在我只有实际的纬度和经度来测试它。

Sub forecast_weather()
    Dim cityLon As String
    Dim cityLat As String

    cityLat = "41.8781"
    cityLong = "87.6298"

    ActiveWorkbook.Worksheets("Chicago Weather").Select

    With ActiveSheet.QueryTables.Add(Connection:= _
         "URL;http://forecast.weather.gov/MapClick.php?lat="&cityLat&"&lon=-
         "&cityLong&"&unit=0&lg=english&FcstType=text&TextType=2", _
         Destination:=Range("$A$1"))
         .Name = "q?s=usdCAd=x_1"
         .FieldNames = True
         .RowNumbers = False
         .FillAdjacentFormulas = False
         .PreserveFormatting = True
         .RefreshOnFileOpen = False
         .BackgroundQuery = True
         .RefreshStyle = xlInsertDeleteCells
         .SavePassword = False
         .SaveData = True
         .AdjustColumnWidth = True
         .RefreshPeriod = 0
         .WebSelectionType = xlEntirePage
         .WebFormatting = xlWebFormattingNone
         .WebPreFormattedTextToColumns = True
         .WebConsecutiveDelimitersAsOne = True
         .WebSingleBlockTextImport = False
         .WebDisableDateRecognition = False
         .WebDisableRedirections = False
         .Refresh BackgroundQuery:=False
    End With
End Sub

2 个答案:

答案 0 :(得分:1)

&

周围需要空格
With ActiveSheet.QueryTables.Add(Connection:= _
   "URL;http://forecast.weather.gov/MapClick.php?lat=" & cityLat & _
    "&lon=" & cityLong & "&unit=0&lg=english&FcstType=text&TextType=2", _
        Destination:=Range("$A$1"))

答案 1 :(得分:0)

这让芝加哥天气

Sub forecast_weather()
    Dim cityLon As String
    Dim cityLat As String

    cityLat = "41.8781"
    cityLong = "-87.6298"

    With Worksheets("sheet1").QueryTables.Add( _
            Connection:="URL;http://forecast.weather.gov/MapClick.php?" _
            & "lat=" & cityLat _
            & "&lon=" & cityLong _
            & "&unit=0&lg=english&FcstType=text&TextType=2", Destination:=Range("$A$1"))
        .Name = "q?s=usdCAd=x_1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Sub