【问题标题】:Excel 2013 VBA Web Query connection string containing variableExcel 2013 VBA Web 查询连接字符串包含变量
【发布时间】:2014-01-16 03:49:13
【问题描述】:

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

【问题讨论】:

  • 考虑使用 xmlhttp 而不是 webquery。此代码从39页下载数据link

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


【解决方案1】:

将 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 Method

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 2013-12-29
    • 2018-10-16
    • 1970-01-01
    • 2013-09-15
    • 2019-11-12
    相关资源
    最近更新 更多