Excel 如何让VBA基于URL中的两个变量导出CSV文件?

Excel 如何让VBA基于URL中的两个变量导出CSV文件?,excel,vba,csv,Excel,Vba,Csv,我希望代码在qurl中同时运行XNYS和XNAS,并提取CSV文件。有什么办法吗?我将stockTicker作为一个变量,但希望它在出错时运行其他XNAS。不确定您的意思。那么,您想再次运行另一个URL的代码吗?如果是这样,只需更改XNYS或XNAS的URL或您想要的任何内容,并将代码复制到sub的末尾。如果您想做得更复杂一点,还可以循环URL数组。但就是这样。还是我完全偏离了主题?我已经弄明白了。我不知道我在做什么,但到目前为止,我已经设法使一切工作,而不知道一点关于VBA。互联网是惊人的。是

我希望代码在qurl中同时运行XNYS和XNAS,并提取CSV文件。有什么办法吗?我将
stockTicker
作为一个变量,但希望它在出错时运行其他XNAS。

不确定您的意思。那么,您想再次运行另一个URL的代码吗?如果是这样,只需更改XNYS或XNAS的URL或您想要的任何内容,并将代码复制到sub的末尾。如果您想做得更复杂一点,还可以循环URL数组。但就是这样。还是我完全偏离了主题?我已经弄明白了。我不知道我在做什么,但到目前为止,我已经设法使一切工作,而不知道一点关于VBA。互联网是惊人的。是的,你说到了主题。我想循环一下,但不知道怎么做。不过我还是想出来了。谢谢
Sub DownloadKeyRatios(ByVal stockTicker As String, ByVal StartDate As Date, ByVal EndDate As Date, ByVal DestinationCell As String, ByVal freq As String)
    Dim qurl As String
    Dim StartMonth, StartDay, StartYear, EndMonth, EndDay, EndYear As String
    Dim C As WorkbookConnection
    StartMonth = Format(Month(StartDate) - 1, "00")
    StartDay = Format(Day(StartDate), "00")
    StartYear = Format(Year(StartDate), "00")

    EndMonth = Format(Month(EndDate) - 1, "00")
    EndDay = Format(Day(EndDate), "00")
    EndYear = Format(Year(EndDate), "00")
    qurl = "URL;http://financials.morningstar.com/ajax/exportKR2CSV.html?&callback=?&t=XNYS:" + stockTicker + "&region=usa&culture=en-US&cur=&order"

    On Error GoTo ErrorHandler:
    With ActiveSheet.QueryTables.Add(Connection:=qurl, Destination:=Range(DestinationCell))
        .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 = "20"
        '    .WebPreFormattedTextToColumns = True
        '    .WebConsecutiveDelimitersAsOne = True
        '    .WebSingleBlockTextImport = False
        '    .WebDisableDateRecognition = False
        '    .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
ErrorHandler:

End Sub