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 do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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