VBA help pls! how to copy (href) web link into excel cells?

dawgnuts

New Member
Joined
Jun 11, 2014
Messages
4
Hi guys, big fan of this website.

As I am very new to VBA, could someone please help me pull a website link from the html code in "view source" of a webpage into my excel spreadsheet cell?

For instance, on this webpage (BBC News - Home), I would like to pull the news title and hyperlink of the news title into my excel, I am able to pull the title into my cells using "sht.Range("A"& RowCount)= ele.innertext". But is there an equivalent method to copy the weblink? I highlighted the part where i am stuck in red (below).

I am currently watching a youtube video explaining how to pull data from webpages into excel via VBA, and my code basically looks like this:




<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">Sub test()

Dim eRow As Long

Dim ele As Object

Set sht = Sheets("Sheet1")
RowCount =1
sht.Range("A"& RowCount)="Title"
sht.Range("B"& RowCount)="Web link"

eRow = Sheet1.Cells(Rows.Count,1).End(xlUp).Offset(1,0).Row

Set objIE = CreateObject("InternetExplorer.Application")

With objIE
.Visible =False
.navigate "http://www.bbc.co.uk/news/"

For Each ele In.document.all

Select Case ele.classname

Case"top-story-header"
RowCount
= RowCount +1

Case"top-story-header"

sht
.Range("A"& RowCount)= ele.innertext

Case"top-story-header"
sht
.Range(B & RowCount)=href link here
End Select

Next ele

End With

Set objIE = Nothing

End Sub
</code>
 
Last edited:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
the code doesnt even work :( but basically how do I amend this part of my code:


Case"above_postlist"

sht
.Range("A"& RowCount)= ele.innertext




so that it will copy the weblink from this part of the html (ignore the smilies)

<:eek:div id="above_postlist" class="above_postlist":eek:>
<:eek:a rel="nofollow" href="http://www.mrexcel.com/forum/newreply.php?p=3836432&noquote=1" class="newcontent_textcontrol" id="newreplylink_top">+ Reply to Thread

<tbody>
</tbody>
 
Last edited:
Upvote 0
After you use objIE.Navigate you need to wait for the page to load. To do this, add the following bit of code after the objIE.Navigate method

Code:
Do While objIE.Busy Or Not objIE.ReadyState = READYSTATE_COMPLETE
    DoEvents
Loop
 
Upvote 0
the code doesnt even work :( but basically how do I amend this part of my code:


Case"above_postlist"

sht
.Range("A"& RowCount)= ele.innertext




so that it will copy the weblink from this part of the html (ignore the smilies)

<:eek:div id="above_postlist" class="above_postlist":eek:>
<:eek:a rel="nofollow" href="http://www.mrexcel.com/forum/newreply.php?p=3836432&noquote=1" class="newcontent_textcontrol" id="newreplylink_top">+ Reply to Thread

<tbody>
</tbody>

No one has any ideas on this? :c
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,680
Members
449,463
Latest member
Jojomen56

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