如何动态添加形状链接并下载相应的CSV文件?

时间:2017-10-04 05:05:09

标签: excel vba excel-vba

我有一个类似“https://example.com.uk/Download/Report/2017-10-01_successful.csv”的链接,我无法下载CSV文件。

我必须通过此链接每天更改日期以下载CSV文件。所以,我尝试了以下代码,其中a,b和c是特定日期的给定输入。

我的要求是:

我想通过更改链接中的日期值来下载此报告。我的代码添加了链接,但无法下载数据。

我该怎么办?是否需要在Excel VBA中引用任何库?

这是我的代码:

Sub Macro2()

    Range("D14:L14").Select
    Sheets("RAW_DATA").Select
    Range("B26").Select

    a = 2017
    b = 10
    c = 3
    ActiveSheet.Shapes.Range(Array("Rectangle 11")).Select
    'Selection.ShapeRange.Item(1).Hyperlink.Delete
    ActiveSheet.Hyperlinks.Add Anchor:=Selection.ShapeRange.Item(1), Address:= _
        "https://example.com.uk/Download/Report/" & a & "-" & b & "-" & c & "_successful.csv"
    Range("D20").Select
    Selection.ShapeRange.Item(1).Hyperlink.Follow NewWindow:=False, AddHistory:=True
    Workbooks.Open Filename:= _
        "https://example.com.uk/Download/Report/" & a & "-" & b & "-" & c & "_successful.csv"
    ActiveWindow.Visible = False
    Windows("2017-10-01_successful.csv").Visible = True
    'Windows("ARC_ASC_TEMPLATE_VBA_V3.xlsm").Activate

End Sub

1 个答案:

答案 0 :(得分:1)

如果这不起作用会发生什么呢?

Sub Macro2()

    Dim wb As Workbook, fName, a, b, c

    a = 2017
    b = 10
    c = 3

    fName = "https://example.com.uk/Download/Report/" & a & "-" & _
            Format(b, "00") & "-" & Format(c, "00") & "_successful.csv"

    Set wb = Workbooks.Open(Filename:=fName)

    Debug.Print wb.Name

End Sub