How to click a button when scraping web page using VBA

Cool_Pacific

New Member
Joined
May 26, 2017
Messages
7
Hi there,

I am trying to scrape a web page, I could read most of the data present at current page for all years (Mar 16, Mar 15, Mar 14, Mar 13, Mar 12).
I want to read data for Previous years also for which I need to click button "Previous Years" on the web page. I would like to get help on how can I click this particular button.
I am not expert in VBA so if someone can throw a piece of code which could click this button then that will be a great help for me.

Thank you!

Below is the code i am using to read web page data:



Function GetMCFinancialData()


Dim XMLReq As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLTable As MSHTML.IHTMLElement
Dim HTMLTables As MSHTML.IHTMLElementCollection
Dim HTMLRow As MSHTML.IHTMLElement
Dim HTMLRows As MSHTML.IHTMLElementCollection
Dim HTMLCell As MSHTML.IHTMLElement
Dim sText As String
Dim rowNum As Long
Dim colNum As Long

On Error GoTo skipAllFinData
XMLReq.Open "GET", "http://www.moneycontrol.com/financials/aartidrugs/balance-sheetVI/AD#AD", False
XMLReq.send

If XMLReq.Status <> 200 Then
'MsgBox "Error " & XMLReq.Status & ": " & XMLReq.StatusText
Exit Function
End If

HTMLDoc.body.innerHTML = XMLReq.responseText


'Current Data
rowNum = 1
Cells(rowNum, "A").Value = "Various Financical Data"
rowNum = rowNum + 1

On Error GoTo skipFinData
If InStr(1, Qurl, "capital-structure") = 0 Then
' If not capital structure page
Set HTMLRows = HTMLDoc.getElementsByClassName("table4")(2).getElementsByTagName("tr")
Else
Set HTMLRows = HTMLDoc.getElementsByClassName("table4")(1).getElementsByTagName("tr")
End If

For Each HTMLRow In HTMLRows
colNum = 1
For Each HTMLCell In HTMLRow.getElementsByTagName("td")
Cells(rowNum, colNum).Value = HTMLCell.innerText
colNum = colNum + 1
Next HTMLCell
rowNum = rowNum + 1
Next HTMLRow


skipFinData:
skipAllFinData:

Set XMLReq = Nothing
Set HTMLDoc = Nothing
Set HTMLTable = Nothing
Set HTMLTables = Nothing
Set HTMLRow = Nothing
Set HTMLRows = Nothing
Set HTMLCell = Nothing

End Function
 

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.
I don't think you can click the button, or interact with the page in anyway, using that method.
 
Upvote 0
Norie,

Thank you for the quick reply!
Yes, current function is not having any code to attempt to click the button on web page. I am very new to VBA programming and I do not know how to implement a button click, so I wanted to get help on that if possible.
 
Upvote 0
Here is the source:

<div style="display:none"><span class="mr_gL_14"><a href='http://www.moneycontrol.com/sensex/bse/sensex-live' title='BSE Sensex' class="mr_gL_14"><strong>SENSEX</strong></a></span> <span class="mr_b_20"><span id="sensex_val_top"></span></span> <span id="bse_img_top"/> </span><span class="mr_b_15"><strong><span id="sensex_change"></span></strong></span> <span class="mr_b_14" id="sensex_pchange"></span> <span class="mr_sep MT5"></span> <span class="mr_gL_14"><a href='http://www.moneycontrol.com/nifty/nse/nifty-live' title='NSE Nifty 50' class="mr_gL_14"><strong>NIFTY</strong></a></span> <span class="mr_b_20"><span id="nifty_val_top"></span></span> <span id="nse_img_top"/></span> <span class="mr_b_15"><strong><span id="nifty_change"></span></strong></span> <span class="mr_b_14" id="nifty_pchange"></span>
</div>
<!-- Not In Use -->
<!-- Not In Use -->
<div style="display:none;" id="fliper_ad_rad_d"><a href="http://pubads.g.doubleclick.net/gampad/clk?id=432343564&iu=/1039154/Moneycontrol/MC_Click_Tracker" target="_new"><img src="http://img-d02.moneycontrol.co.in//images/promo/2017/19jun2017/Flip_Logo_16Jun_118x30.jpg" /></a></div><div style="display:none;" id="tckr_ad_rad_d"><a href="http://pubads.g.doubleclick.net/gampad/clk?id=432343564&iu=/1039154/Moneycontrol/MC_Click_Tracker" target="_blank"><img src="http://img-d01.moneycontrol.co.in//images/promo/2017/19jun2017/Ticker_16Jun_300x22.jpg" /></a></div> <!-- Stock radar end -->
</div>
<div class="PA10">

<div class="FL" style="width:775px;">
<table cellpadding="0" cellspacing="0" border="0">
<tr><td><link rel="alternate" TITLE="MoneyControl.com News" HREF="http://www.moneycontrol.com/rss/latestnews.xml" TYPE="application/rss+xml" /></td></tr>
</table>
<link rel="alternate" title="MoneyControl.com News" href="http://www.moneycontrol.com/rss/latestnews.xml" type="application/rss+xml"><link rel="alternate" title="MoneyControl.com News" href="http://www.moneycontrol.com/rss/latestnews.xml" type="application/rss+xml">
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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