QueryTables.Add 动态 link 引用单元格

QueryTables.Add dynamic link referencing cells

我要用的link是 "https://xxx/xxx/xxx/reporting?start_date=2022-05-15+00&end_date=2022-05-21+00" 而 start_date=Simba1 和 end_date=辛巴2

辛巴 1 和辛巴 2 是日期单元格。

'Download Simba Table'

Sheets("SIMBA Data").Select
Cells.Select
Selection.ClearContents
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;https://xxx/xxx/xxx/reporting?" _
    & "start_date=" & Simba1 _
    & "&end_date=" & Simba2 & "", Destination:=Range("$A"))
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlSpecifiedTables
    .WebFormatting = xlWebFormattingNone
    .WebTables = "1"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

使用Format()

With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;https://xxx/xxx/xxx/reporting?" _
    & "start_date=" & Format(Simba1, "yyyy-mm-dd") _
    & "+00&end_date=" & Format(Simba2, "yyyy-mm-dd") & "+00", _
    Destination:=Range("$A"))
   
    '...
    '...

在 Tim 发布的答案中添加以下内容,解决了我的问题!!

Dim Simba1 As Variant

Simba1 = Range("'Report Controls'!$C").Value

Dim Simba2 As Variant

Simba2 = Range("'Report Controls'!$C").Value