在Excel中通过URL编辑date
我是一个相当新的vba编码,并会欣赏一些帮助,因为我玩一些代码。 我正在尝试编辑一个url,以查找历史汇率的dynamic
原始码:
With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://www.x-rates.com/historical/?from=USD&amount=1&date=2017-04-20", _ Destination:=Range("A1")) .PostText = "currency_exchange_practice"
我的新代码不起作用:
Dim dateday, datemonth, dateyear As Long, fulldate As Variant Columns("A:D").ClearContents dateday = Day(Range("G2")) datemonth = Month(Range("G2")) dateyear = Year(Range("G2")) fulldate = dateyear & "-" & datemonth & "-" & dateday With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://www.x-rates.com/historical/?from=USD&amount=1&date=" & fulldate & "", _ Destination:=Range("A1")) .PostText = "currency_exchange_practice"
它也在删除单元格g2中的所有数据,包括一个button,我所做的不在单元格A:D中。 请,并感谢您的帮助!
我认为这个问题与你使用Month
四月的月份将返回4
而不是04
所以当您将fulldate
传递给2017-4-20
而不是2017-04-20
的url时。 如果我像过去那样fulldate
通过,我什么都没有回来。
要解决这个问题,我会首先声明你的date部分作为string,所以我们可以使用Format
function。 这里一个重要的注意事项是你必须声明每个variables的数据types。 正如你写的那样, dateday
和datemonth
是Variants
因为它们没有明确给出数据types。
Dim dateday As String, datemonth As String, dateyear As String, fulldate As String
那么你可以这样做:
dateday = Format(Day(Range("G2")), "00") datemonth = Format(Month(Range("G2")), "00")
现在, fulldate
将看起来像2017-04-20
,应该工作。
我确实确认你所使用的网站也是一天的两位数字。
我不知道为什么它会删除列G中的任何东西,除非你的查询表在某种程度上与A1放在一起。