Excel VBA宏查找Intranet链接到querytable

时间:2015-08-12 14:38:38

标签: excel vba excel-vba

我正在尝试编写一个宏来将内部网页中的信息提取到Excel工作表上。我正在尝试自动执行此操作,因为URL可能会根据网页上选择的日期和“转移”而发生变化。

编译代码时出现了几个错误。我尝试过几种不同的方法,但似乎没有一种方法可行。

有什么想法吗?

    Sub InfoExtract()

    Dim Symbol As String
    Dim URL As String
    Dim ws1 As Worksheet

    Set ws1 = Sheets("Sheet3")
    ws1.Select

    Symbol = "%20"

    With ActiveSheet.QueryTables.Add(Connection:=Range("link1").Value & Range("day").Value & Symbol & Range("month").Value & Symbol & Range("Year").Value & Symbol & "the_shift=" & Range("shift").Value & "_1", Destination:=Range("$B$4"))
        .Name = "shift_report.asp?the_date=" & Range("day").Value & Symbol & Range("month").Value & Symbol & Range("Year").Value & Symbol & "the_shift=" & Range("shift").Value
        .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
End Sub

1 个答案:

答案 0 :(得分:1)

我说链接不正确,因此您收到错误。其余似乎很好,无论如何都可以使用宏录制器轻松验证。因此,我建议您在使用之前构建并验证链接。

strURL = Range("link1").Value & Range("day").Value & Symbol & Range("month").Value & Symbol & Range("Year").Value & Symbol & "the_shift=" & Range("shift").Value & "_1" ' This is copied from your code and cannot be verified
Debug.Print strURL 'copy this into the IE and see if it works
With ActiveSheet.QueryTables.Add(Connection:=strURL, Destination:=Range("$B$4"))
        .Name = "TestName"
'... the rest of your code as is ...