VBA to Loop through and Copy Tables from HTML on Websites

PShingadia

New Member
Joined
Aug 5, 2015
Messages
47
Hi All:

Really struggling with this and any help would be appreciated!

The following code works at copying a single table from a website and pasting it directly to worksheet. How would you adapt this to copy several tables from a website in a loop with each table on a new sheet?


Option Explicit


' Add reference to Microsoft Internet Controls
' Add reference to Microsoft HTML Object Library
' Add reference to Microsoft Forms 2.0 Object Library


Private Const url As String = "http://www.livecharts.co.uk/share_prices/top_uk_shares.php"


Sub CopyClipboard()
Dim ie As SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument
Dim tables As MSHTML.IHTMLElementCollection
Dim table As MSHTML.HTMLTable
Dim clipboard As MSForms.DataObject


Set ie = New SHDocVw.InternetExplorer


With ie
.Visible = True
.Navigate url


While .Busy Or .ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend


Set doc = .Document
Set tables = doc.getElementsByTagName("table")
Set table = tables(0)
Set clipboard = New MSForms.DataObject


clipboard.SetText table.outerHTML
clipboard.PutInClipboard
ActiveSheet.Paste


.Quit
End With
End Sub
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
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