包含变量的Excel 2013 VBA Web Query连接字符串

时间:2014-01-16 03:49:14

标签: vba excel-vba excel-2013 excel-web-query excel

我在以下行收到运行时错误'1004'错误。

With ActiveSheet.QueryTables.add(Connection:=connstring, Destination:=Range("$b$2"))
Destination:=Range("$b$1"))

变量connstring似乎导致了这个问题。如何在此连接语句中正确使用变量名?

非常感谢帮助


Sub add()

For x = 1 To 58000

 Worksheets("PAGES").Select
 Worksheets("PAGES").Activate

 connstring = "http://www.name-list.net/russia/1"

 With ActiveSheet.QueryTables.add(Connection:=connstring, Destination:=Range("$b$2"))
 Destination:=Range("$b$1"))

    .Name = "1"
    .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 = "2"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

Next x

1 个答案:

答案 0 :(得分:2)

将connstring更改为 - >

connstring = "URL;http://www.name-list.net/russia/1"

更好 - >

Dim connstring As String
connstring = "URL;http://www.name-list.net/russia/1"

该方法的MSDN doco为QueryTables.Add Method

我无法保证您的其余代码能够按预期工作。为什么要循环58000次?