VBA中的查询表错误

时间:2018-08-13 11:16:10

标签: excel vba excel-vba

我正在从网站上的excel表格中获取数据,但出现错误

  

对象“变量或未设置块变量”

在这部分代码中。您可以在下面找到我尝试过的全部代码。

With ActiveSheet.QueryTables.Add(Connection:= _
     "URL;http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?" & url & "" _
     , Destination:=range("$A$1"))

下面是我到目前为止尝试过的代码。

Sub Macro1()
    Dim mf As Integer
    Dim to_date As String
    Dim from_date As String
    Dim range As range
    Dim url As String

    to_date = Format(Sheets("sheet1").Cells(5, 10).Value, "dd-mmm-yyyy")

    from_date = Format(Sheets("sheet1").Cells(8, 10).Value, "dd-mmm-yyyy")

    mf = Application.WorksheetFunction.VLookup(Sheets("sheet1").ComboBox1.Value, 
    Worksheets("sheet1").range("A:B"), 2, False)



    url = "mf=" & mf & "&tp=1&frmdt=" & to_date & "&todt=" & from_date

      With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?" & url & "" _
        , Destination:=range("$A$1"))
        .Name = _
        "DownloadNAVHistoryReport_Po.aspx?mf=3&tp=1&frmdt=04-Mar-2014&todt=13-Aug-2018"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    Columns("A:A").Select
    Selection.TextToColumns Destination:=range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)), _
        TrailingMinusNumbers:=True
End Sub 

0 个答案:

没有答案