将数据从Web导入Excel 2010工作表

时间:2013-02-17 22:45:40

标签: windows excel vba excel-vba excel-2010

我正试图在win 7上将数据从网页导入Excel工作表。

但是,我不知道如何完成左侧的标记。

Sub Ex2Macro()

    Ex2 from Macro1 Macro
    ' Macro changed 17/07/2007 by Dr. B. I. Czaczkes
    Dim qt As QueryTable
    Dim temp As Variant ' Double
    Set qt = ActiveSheet.QueryTables.Add(Connection:= _
      "URL;http://finance.yahoo.com/q?s=GBPUSD=X", Destination:=ActiveCell.Range("A1"))
    With qt
    .Name = "query1"
    .BackgroundQuery = False
    .SaveData = True
    .WebSelectionType = xlSpecifiedTables
    .WebFormatting = xlWebFormattingNone
    .WebTables = "14"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .Refresh BackgroundQuery:=False
  End With
  With qt.ResultRange
    .ClearContents
    .Range("a1").Value = "USD/GBP"
    .Range("b1").Value = ?     ' here, what I should put so that the queried result             
                               ' is printed ? 
  End With
End Sub

提供交换结果的另一段代码。

Sub Ex3Macro()
  Dim qt As QueryTable
  ' Dim temp As Double
  Dim temp As Variant
  temp = ActiveCell.Value
  Set qt = Worksheets("temp").QueryTables.Add(Connection:= _
    "URL;http://finance.yahoo.com/currency/convert?amt=" & _
    temp & _
    "&from=USD&to=GBP&submit=Convert" _
    , Destination:=Worksheets("Temp").Range("a1"))
  With qt
    .Name = "query2"
    .BackgroundQuery = False
    .SaveData = True
    .WebSelectionType = xlSpecifiedTables
    .WebFormatting = xlWebFormattingNone
    .WebTables = "13"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .Refresh BackgroundQuery:=False
  End With
  ActiveCell.Range("b1").Value = qt.ResultRange.Range("e3").Value
End Sub

但是,没有打印结果。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试选择Microsoft WinHTTP Services,版本5.1参考,然后使用:

Sub HTTPTest()

Dim httpRequest As WinHttpRequest
Dim URL As String, strHTML As String

If httpRequest Is Nothing Then
    Set httpRequest = New WinHttp.WinHttpRequest
End If

URL = "http://finance.yahoo.com/q?s=GBPUSD=X"

httpRequest.Open "GET", URL, True
httpRequest.Send
httpRequest.WaitForResponse
strHTML = httpRequest.ResponseText
Set httpRequest = Nothing

'Parse strHTML now to get your value

End Sub