VBA动态日期在API URL中

问题描述 投票:0回答:1

我有一个动态更新的API,以便用户可以在电子表格中输入开始日期和结束日期,我的宏将回读该特定日期范围的数据。

我遇到的问题是,在API URL中,StartDate和EndDate参数必须采用yyyy-mm-dd格式作为字符串。

我已经尝试过URL = "https:// ...&StartDate = Format(Date(),"yyyy-mm-dd") & EndDate=Format(Date(),"yyyy-mm-dd")&..."(这是...用于URL之前和之后的事情)。

我正在查看的URL类型的示例是:

https://www.googleapis.com/analytics/v3/data/ga?ids=ga:12345&startdate=2008-10-01&end-date=2008-10-31&metrics=ga:sessions,ga:bounces

我也玩过在URL字符串中添加额外的引号,但我似乎无法让它工作。

我一直被告知日期没有被识别,因此如果我硬编码日期,我只能运行代码。

有什么建议?

vba api date
1个回答
0
投票

我注意到发布的代码中有一些问题。 &是VBA中的连接运算符。你需要将它包含在""中,以确保你将&符号作为字符串返回,而不是将字符串连接在一起。

我添加了一些示例代码,希望能够说明这个想法并让您重新启动并运行。如果TruecreatedURL相等,代码应打印出testURL,否则打印False


Option Explicit

Public Sub FormatExample()
    'This is the example provided
    Dim testURL As String
    testURL = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:12345&" & _
              "startdate=2008-10-01&end-date=2008-10-31&metrics=ga:sessions,ga:bounces"

    'This is a built string example
    Dim createdURL As String
    createdURL = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:12345" & _
                 "&startdate=" & Format(#10/1/2008#, "yyyy-mm-dd") & _
                 "&end-date=" & Format(#10/31/2008#, "yyyy-mm-dd") & _
                 "&metrics=ga:sessions,ga:bounces"

    'Print out if they are equal
    Debug.Print createdURL = testURL

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.