Excel VBAmacros查找Intranet链接到查询表

我正在尝试编写一个macros来将信息从Intranet页面提取到Excel工作表上。 我试图自动执行此操作,因为url可能会根据date和网页上select的“移位”而改变。

编译我的代码时出现了一些错误。 我已经尝试了几种不同的方法,但他们都没有工作。

有什么想法吗?

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 

我会说链接是不正确的,因此你得到的错误。 其余的似乎很好,可以很容易地使用macroslogging器进行validation。 因此,我build议您在使用之前build立并validation链接。

 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 ...