VBA loop through Webpage Tables

JimboP77

New Member
Joined
Mar 31, 2013
Messages
29
Hi all

Can someone please give me some simple VBA code that loops through all the tables in a webpage?

I would like to the debug.print the contents of the tables.

Thanks in advance.

Regards

Jim
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Onother option should be "disabling BBCode", but I dont see such a command.
Try attaching a txt file, using a file sharing facility, for example dropbox or filedropper.com

Bye
 
Upvote 0
Without any chance of testing:
Code:
Function GetTabbb() As Boolean
Dim myColl As Object, my2Coll As Object
'
myURL = "http://your web address"           '<<<<<<<<<<<
Set IE = CreateObject("InternetExplorer.Application")
   
With IE
    .navigate myURL
    .Visible = True
    Do While .Busy: DoEvents: Loop    'Attesa not busy
    Do While .readyState <> 4: DoEvents: Loop 'Attesa documento
End With
'
myStart = Timer  'attesa addizionale
Do
    DoEvents
    If Timer > myStart + 1 Or Timer < myStart Then Exit Do
Loop
'
Set myColl = IE.document.getElementsByTagName("TABLE")
For Each myitm In myColl
    If myitm.classname = "main" Then
        Set my2Coll = myitm.getElementsByTagName("td")
            If my2Coll(7).innerText = "Yes" Then
                GetTabbb = True
            End If
    End If
    Exit For
Next myitm
'
''Stop    'SEE TEXT
'Chiusura IE
IE.Quit
Set IE = Nothing
End Function
Fill the line marked <<<<
Then call the function: it will return True if the filed contains Yes.

For example:
Code:
YNo = GetTabbb
if YNo then
    'What to do if YES
end if
Bye
 
Last edited:
Upvote 0
The code is based on the limited portion of html source code you published.
It searches the first table with Class="main" (but I dont know if the page contains other tables with class="main), then in the table it checks for content of 8th "td" item.
You migth try this variant of the For Each myitm / Next myitm loop:
Code:
For Each myitm In myColl
Debug.Print myitm.className
    If myitm.className = "main" Then
        Set my2Coll = myitm.getElementsByTagName("td")
            If my2Coll(7).innerText = "Yes" Then
                GetTabbb = True
                Exit For
            End If
    End If
'    Exit For
Next myitm
Debug.Print GetTabbb, myColl.Length, my2Coll.Length
With this variant we will not look for "the first" table having class="main", but "any table" with that class.
I also added some debug instructions, so that the code will record in the "Immediate window" of vba which are the classnames of the scanned Tables and the True /False returned by the code.
Have a look at the Immediate window of vba (Menu /View /Immediate window) and post what is written there, if the function still do not return True if the field contains a Yes.

Bye
 
Upvote 0
Hi Anthony

I managed to solve it with the below code as released the part of the table I was interested in was always in the same position! Thanks for your help though.

Code:
Set colTR = ie.document.getElementsByTagName("TR")

x = 0


For Each tr In colTR


    Set coltd = tr.getElementsByTagName("TD")


    For Each td In coltd
            x = x + 1
            
    If x = 18 Then
        If td.innerText = "Yes" Then
          TVT = 1
        Else
          TVT = 0
        End If
        
        GoTo Jump10
        
    End If
    
    Next td


Next tr
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top