我有一个我想要动态更新的API,以便用户可以在电子表格中输入开始日期和结束日期,我的宏将撤回该特定日期范围的数据。
我遇到的问题是,在API网址中,StartDate和EndDate参数必须采用yyyy-mm-dd格式作为字符串。
我已经尝试过URL = "https:// ...&StartDate = Format(Date(),"yyyy-mm-dd") & EndDate=Format(Date(),"yyyy-mm-dd")&..."
(...是针对网址之前和之后的内容)。
我正在查看的网址类型的示例是:
我还在网址字符串中添加了额外的引号,但我似乎无法让它发挥作用。
我一直被告知日期并未被识别,因此如果我对日期进行硬编码,我只能运行代码。
有什么建议吗?
答案 0 :(得分:0)
我注意到发布的代码中有一些问题。 &
是VBA中的连接运算符。您需要将其括在""
中,以确保将&符号作为字符串返回,并且不会将字符串连接在一起。
我添加了一些示例代码,希望能够说明这个想法并让您备份并运行。如果True
和createdURL
相等,则代码应打印出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