2014-01-16 121 views
3

我在下面一行中得到运行时错误'1004'错误。包含变量的Excel 2013 VBA Web查询连接字符串

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 
+0

考虑使用xmlhttp而不是webquery。此代码从39页[**链接**]下载数据(http://stackoverflow.com/questions/20832017/import-tables-from-a-webpage-to-excel/20832886#20832886) – Santosh

回答

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次?