3

我在下一行收到运行时错误“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
4

1 回答 1

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 文档是QueryTables.Add 方法

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

于 2014-01-16T04:20:03.960 回答