使用vba比较Web表数据

时间:2014-03-01 21:37:12

标签: vba excel-vba excel

我已经使用来自this page的vba将网表提取到excel中,现在我需要从定价表中获取第一个产品和特定产品(CameraNU.nl)的位置。我有以下代码来获取表

 Sub TableExample()
        Dim IE As Object
        Dim doc As Object
        Dim strURL As String

         If Range("B2").Value <> "NA" Then

        strURL = "http://www.kieskeurig.nl/spiegelreflexcamera/nikon/d3200_body/prijzen/bezorgen/1052716#prijzen"

        ' replace with URL of your choice

        Set IE = CreateObject("InternetExplorer.Application")
        With IE

            .navigate strURL
            Do Until .readyState = 4: DoEvents: Loop
                Do While .Busy: DoEvents: Loop
                    Set doc = IE.document
                    GetAllTables doc

                    .Quit
                End With
                Else
    Range("B1").Value = "Shopping Channel"
    Range("C1").Value = "Competitor Website"
    Range("D1").Value = "Competitor Price"

                Range("B2").Value = "kieskeurig.nl"
                Range("C2").Value = "Product Not Available"
    Range("D2").Value = "Product Not Available"
    Range("E2").Value = "Product Not Available"
    Range("F2").Value = "Product Not Available"
                End If

            End Sub




       Sub GetAllTables(doc As Object)

     ' get all the tables from a webpage document, doc, and put them in a new worksheet

    Dim ws As Worksheet
    Dim rng As Range
    Dim tbl As Object
    Dim rw As Object
    Dim cl As Object
    Dim tabno As Long
    Dim nextrow As Long
    Dim i As Long

    Set ws = Sheets("Sheet1")

On Error GoTo Err1:


    For Each tbl In doc.getElementsByTagName("TABLE")
        tabno = tabno + 1
        nextrow = nextrow + 1
        Set rng = ws.Range("B" & nextrow)
        rng.Offset(, -1) = "Table " & tabno
        For Each rw In tbl.Rows
            colno = 1
            For Each cl In rw.Cells
                If colno = 1 And nextrow > 1 Then
                    Set classColl = doc.getElementsByClassName("shopLogoX")
                    Set imgTgt = classColl(nextrow - 2).getElementsByTagName("img")
                    rng.Value = imgTgt(0).getAttribute("alt")
                Else
                    rng.Value = cl.innerText
                End If
                Set rng = rng.Offset(, 1)
                i = i + 1
                colno = colno + 1
            Next cl
            nextrow = nextrow + 1
            Set rng = rng.Offset(1, -i)
            i = 0
        Next rw
    Next tbl



Err1:

Call comp

End Sub


Sub comp()
Dim i As Integer

Dim j As Integer

Dim k As String

Dim l As String

Dim m As String

l = Range("B2").Value
m = Range("F2").Value


i = 2

For i = 2 To 50

If Range("B" & i).Value = "Foto Konijnenberg" Then

j = i

k = Range("F" & i).Value


End If

Next i

Range("B1:h50").Value = ""


Range("k24").Value = j

Range("l25").Value = k

Range("B1").Value = "Shopping Channel"
Range("C1").Value = "Competitor Website"
Range("D1").Value = "Competitor Price"
Range("E1").Value = "Our Position"
Range("F1").Value = "Our Price"


Range("B2").Value = "kieskeurig.nl"
Range("C2").Value = l
Range("D2").Value = m

Range("E2").Value = Range("K24").Value
Range("F2").Value = Range("L25").Value



Range("K24").Value = ""

Range("L25").Value = ""
End Sub

如果您运行此宏,您将获得我想要的确切结果,但我需要比较价格而不使用该comp子例程。任何人都可以帮助我...

0 个答案:

没有答案