Excel VBA code to fetch value/maltiple value from web site - vba

I am trying to fetch data from http://www.dsebd.org/displayCompany.php?name=LINDEBD by using VBA code in excell.
trying to get the follwing data from this links
Total No. of Outstanding Securities
Sector
Company Name
am trying with,
Sub GetData()
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://www.dsebd.org/displayCompany.php?name=LINDEBD"
With IE
.Visible = True
.navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End With
Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading
Set Results = IE.document.getElementsByTagName("table")
For Each itm In Results
If itm.classname = "lgn" Then
dd = itm.getAttribute("href")
Exit For
End If
Next
' if you wnat to click the link
itm.Click
' otherwise
'Range("a1").Value = dd
MsgBox dd
End Sub
Is it possible to collect data from this site

First, you have to iterate the table(table), then you iterate the rows(tr), then within that you iterate the cells(td). These are not links on this page.
Note: The following will get you the value you seek, but this code will be subject to constant change if the website changes.
Sub GetData()
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://www.dsebd.org/displayCompany.php?name=LINDEBD"
With IE
.Visible = True
.navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End With
Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading
Set Results = IE.document.getElementsByTagName("table")
For Each itm In Results
Set rowAlt = IE.document.getElementsByClassName("alt")
For Each itmTR In rowAlt
Set tdValues = itmTR.getElementsByTagName("td")
If tdValues.Item(0).innerText = "Total No. of Outstanding Securities" Then
MsgBox tdValues.Item(1).innerText 'the value you seek
Exit Sub 'don't continue this loop
End If
Next
Next
End Sub

Related

Getting 438 error while entering data on textbox on IE. This text box is on another TAB

I am doing some automation on IE browser. On this i am logging into the web application and after logging when i am clicking on botton the web opens a new tab where i need to enter the data onto the textbox but here i am getting an error 438. below is my code. please help me out. i highlighted the line in bold where i am getting an error. The last one i am clicking on end tab and i am getting an error on that
Sub upload()
Dim myValue As Variant
Dim IETab1Number As Integer
Dim IETab2Number As Integer
Dim IETab3Number As Integer
' open IE, navigate to the desired page and loop until fully loaded
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://englogin.sbc.com/ELP/"
With IE
.Visible = True
.Navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End With
' Input the userid and password
IE.document.getElementById("GloATTUID").Value = "Zf9775"
IE.document.getElementById("GloPassword").Value = "MrsCooper$99"
' Click the "Login" button
IE.document.getElementById("GloPasswordSubmit").Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
' Click the "Continue" button
IE.document.getElementById("successButtonId").Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
' Click the "MIC" button
IE.document.getElementById("btnMechanizedInventoryCreation").Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
Set html = IE.document
html.querySelector("a[href='#MIC']").Click
IE.document.querySelector("[name=telco11iProjectNumber]").Value = "123456"
' Enter JOB Number" button
'myValue = InputBox("Give me some input")
End Sub

Sending a value of a mergefield from to a website

I have a mailmerg of patient specific information that is merged into an MSword document. I need to upload it to my hospital document repository. The repository puts the document in the correct place on our server by using the patient identification number. This patient identification number is one of the fields within the mail merge document. I am trying to send the patient id to the webpage without manually typing the ID into the web page. Can anyone help? I have tried to use VBA but I can't quite get the code correct
*Sub Macro1()
'
' Macro1 Macro
'
'Sub IE Automated
Dim ie As InternetExplorer
Dim sURL As String
sURL = "http://cedupload.southend.nhs.uk/"
Set ie = New InternetExplorer
With ie
.Top = 0
.Left = 0
.Width = 1000
.Height = 750
.AddressBar = False
.StatusBar = False
.ToolBar = 0
.Visible = True
.Navigate sURL
Do Until Not .Busy And .ReadyState = 4
DoEvents
Loop
.Document.all("PatientIdentifier") = ThisDocument.MailMerge.Fields.Item.Copy(Unit_No_)
Application.wait Now + TimeSerial(0, 0, 5)
End With
Do Until Not ie.Busy
DoEvents
Loop
ie.Document.all("btn btn-default").Click
Set ie = Nothing
Err.Clear
End Sub*

vba scrape non-static web table data

I'm trying to create a macro that scrape and import tables from a web page,
More specifically, I want to get two tablestables pointed by arrows, please ignore the text in the table if it doesn't make sense, I translated using google. These tables are updated automatically so I used IE approach(by #ron), didn't scrape any data. I'm exhausted, can anyone please help me? I'm a vba newbie, appreciate any help.
Sub test()
' open IE, navigate to the website of interest and loop until fully loaded
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://www.neeq.com.cn/static/statisticdata.html"
With IE
.Visible = False
.navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not IE.busy And IE.ReadyState = 4
DoEvents
Loop
End With
Set tbl = IE.Document.getElementsByTagName("table")
For Each itm In tbl
i = 1
For Each itm2 In itm.Rows
For Each cell In itm2.Cells
ActiveSheet.Cells(i, 2) = cell.innerText
i = i + 1
Next
Next
Next
end sub()
You must select "tr" from your table object For Each Rows
As you use IE, non statix should be return (javascript is executed)
i = 1
For Each itm2 In tbl.getElementsByTagName("tr")
test with debug.print
you select html then tables -> tr -> td
Sub test()
' open IE, navigate to the website of interest and loop until fully loaded
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://www.neeq.com.cn/static/statisticdata.html"
With IE
.Visible = False
.navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not IE.busy And IE.ReadyState = 4
DoEvents
Loop
End With
Set tbl = IE.Document.getElementsByTagName("table")
For Each itm In tbl
i = 1
For Each itm2 In itm.getElementsByTagName("tr")
For Each cell In itm2.getElementsByTagName("td")
ActiveSheet.Cells(i, 2) = cell.innerText
i = i + 1
Next
Next
Next
end sub

Select Drop Down from Web Page with IE object

Have the below code for a drop down selection in VBA .
When I run this I get the error on the bold line saying "Runtime error 91 - Object variable or with block variable not set " ..New to VBA ....
Sub NACDP()
' open IE, navigate to the desired page and loop until fully loaded
Set ie = CreateObject("InternetExplorer.Application")
my_url = "https://cdeployna.cognizant.com/"
With ie
.Visible = True
.Navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
End With
' Input the userid and password
ie.Document.getElementById("loginControl_UserName").Value = ""
ie.Document.getElementById("loginControl_Password").Value = ""
' Click the "Login" button
ie.Document.getElementById("loginControl_LoginButton").Click
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
ie.Document.getElementById("ctl00_ddlRoles").selectedindex = 1
ie.Document.getElementById("ctl00_ddlRoles").FireEvent ("onchange")
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
ie.Document.getElementById("ctl00_ContentBody_searchCDPList_ddlFieldName").selectedindex = 1
ie.Document.getElementById("ctl00_ContentBody_searchCDPList_ddlFieldName").FireEvent ("onchange")
ie.Document.getElementById("ctl00_ContentBody_searchCDPList_txtValue").Value = "Java"
' Click the "Search" button
ie.Document.getElementById("ctl00_ContentBody_searchCDPList_btnSearch").Click
End Sub
It seems that the "Do Until Not ie.Busy And ie.readyState = 4" loop doesn't really do what it's supposed to do. After getting the error, if you press Debug and then Continue the code, it will complete successfully. So, what is happening is that the element you're searching for still doesn't exist when the line first executes. You could patch this up with something like the following before the problem line:
Do While ie.Document.getElementById("ctl00_ContentBody_searchCDPList_ddlFieldName") Is Nothing
DoEvents
Loop
This will loop until the element is found, then the code continues successfully.

Scraping dynamic web page with VBA based on XMLHTTP object

I have problem with scraping table data from this page [http://www.eex.com/en/market-data/power/derivatives-market/phelix-futures]. I use this code, but do not scrape any data:
Public Sub ScrapTableData()
Dim sURL As String
Dim XMLHttpRequest As XMLHTTP
Dim HTMLDoc As New HTMLDocument
Dim elc As HTMLHtmlElement
Dim i As Integer
sURL = "http://www.eex.com/en/market-data/power/derivatives-market/phelix-futures"
Set XMLHttpRequest = New MSXML2.XMLHTTP
XMLHttpRequest.Open "GET", sURL, False
XMLHttpRequest.responseXML.async = False
XMLHttpRequest.send
Do While XMLHttpRequest.Status <> 200
DoEvents
Loop
While XMLHttpRequest.ReadyState <> 4
DoEvents
Wend
HTMLDoc.body.innerHTML = XMLHttpRequest.responseText
' Tables
Dim tbl As HTMLTable, row As HTMLTableRow, cell As HTMLTableCell
i = 1
For Each tbl In HTMLDoc.getElementsByTagName("table")
For Each row In tbl.Rows
For Each cell In row.Cells
ActiveSheet.Cells(i, 5) = cell.innerText
i = i + 1
Next
Next
Next
End Sub
My code does not find HTML table tags.
Also, if I use this part of code, do not list all HTML tags (for example HTML DIV tag) and HTML that describes 6 buttons:
i = 0
Dim elc As HTMLHtmlElement
For Each elc In HTMLDoc.all
Worksheets("Tables").Range("A1").Offset(i, 0) = elc.tagName
i = i + 1
Next
6 buttons: Year, Quarter, Month,..., Day
I need to simulate click on them to display (scrape) different tables' data.
I don't think the XMLHTTP approach will work in this case, you need to open IE. The following code will do this. You may need to modify the loop to put data in your worksheet, I didn't tinker with this. At the end, I've also placed some code that will change the tabs. Hope this helps
Sub test()
' open IE, navigate to the website of interest and loop until fully loaded
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://www.eex.com/en/market-data/power/derivatives-market/phelix-futures"
With IE
.Visible = True
.navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End With
' Collect data from tables
Set tbl = IE.document.getElementsByTagName("table")
For Each itm In tbl
i = 1
For Each itm2 In itm.Rows
For Each cell In itm2.Cells
ActiveSheet.Cells(i, 5) = cell.innertext
i = i + 1
Next
Next
Next
' Click on the 6 buttons, substitute "week", "year", etc. for the button you want to click
Set Results = IE.document.getElementsByTagName("a")
For Each itm In Results
If InStr(1, itm.innertext, "month", vbTextCompare) > 0 Then
itm.Click
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
Exit For
End If
Next
' Do whatever is next
End Sub