我的Web查询在Excel中工作,但在VBA中不工作

时间:2014-02-18 22:36:59

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

我可以从excel电子表格运行Web查询但是当我输入VBA时它没有数据。 以下是一行数据的样子,日期全部在一列中。这个日期前面有一个我要删除的引用,但如果必须的话,我会在VBA内完成。

Monday, February 17, 2014   5   1   1

以下是代码段:

DownloadURL = "http://www.wilottery.com/lottogames/pick3Allhistory.aspx/pick3history.xls"

On Error Resume Next 
With Ws1.QueryTables.Add(Connection:=DownloadURL, Destination:=Ws1.Range("$A$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 = "20"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
ActiveWindow.SmallScroll Down:=-12

Ws1.Range("A1").TextToColumns Destination:=Ws1.Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, _
FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1))

1 个答案:

答案 0 :(得分:2)

试试这段代码:

DownloadURL = "http://www.wilottery.com/lottogames/pick3Allhistory.aspx/pick3history.xls"
With Sheet1.QueryTables.Add(Connection:= _
    "URL;" + DownloadURL, Destination:=Sheet1.Range("$A$1"))
    .Name = "q?s=usdCAd=x_1"
    .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