I am currently trying to use VBA to scrape a particular figure held in a particular table on a certain website. Below is the HTML code surrounding it from the inspect element panel in my browser.
<tr class="cmeRowBandingOff cmeTableRowHighlight">
<th scope="row">MAR 15</th>
<td>2056.50</td>
<td>2062.50</td>
<td>2042.25</td>
<td>2043.25</td>
<td><span>-12.50</span></td>
<td>2044.00</td>
<td class="cmeTableRight">1,351,989</td>
<td class="cmeTableRight">2,701,326</td>
</tr>
I have the VBA code written that will extract the whole table, from "MAR 15" all the way to "2,701,326" - however I only wish to extract the figure "2044.00" into a cell/message box in excel.
My current code is as follows:
Private Sub CommandButton1_Click()
Dim IE As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = False
' URL to get data from
IE.Navigate "http://www.cmegroup.com/trading/equity-index/us-index/" _
& "e-mini-sandp500_quotes_settlements_futures.html"
' Statusbar
Application.StatusBar = "Loading, Please wait..."
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 5, Now)
Loop
Application.StatusBar = "Searching for value. Please wait..."
Dim dd As String
dd = IE.Document.getElementsByClassName("cmeRowBandingOff")(0).innerText
MsgBox dd
' Show IE
IE.Visible = True
' Clean up
Set IE = Nothing
Application.StatusBar = ""
End Sub
I know I need to change this:
dd = IE.Document.getElementsByClassName("cmeRowBandingOff")(0).innerText
but am unsure as to how to go about it.
Could someone kindly help me alter the VBA code to get the result of just 2044.00 on it's own?
Something like:
Dim rw, dd
Set rw = IE.Document.getElementsByClassName("cmeRowBandingOff")(0)
dd = rw.getElementsByTagName("td")(5).innerText
'or
dd = rw.childNodes(6).innerText
'or
dd = rw.Cells(6).innerText
Related
In need few help please in my VBA code:
Private Sub CommandButton1_Click()
Dim IE As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncomment Next line To see form results
IE.Visible = False
' URL to get data from
IE.Navigate "https://www.avanza.se/aktier/om-aktien.html/5247/investor-b"
' Statusbar
Application.StatusBar = "Loading, Please wait..."
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Application.StatusBar = "Searching for value. Please wait..."
Dim dd As String
dd = IE.Document.getElementsByClassName(" ")(0).innerText
MsgBox dd
' Show IE
IE.Visible = True
' Clean up
Set IE = Nothing
Application.StatusBar = ""
End Sub
I want to extract a value from a webpage which is in my case (computer 17) as mention the image by using VBA macro.
I already followed this link (Trying to extract ONE value from a webpage with VBA in Excel) and it work but in my case I have multiple class inside each other.
Thank you very much
This is something to get you started with the InnerText case:
Dim dd As Object
Set dd = IE.Document.getElementsByTagName("tr")
Dim obj As Object
For Each obj In dd
Debug.Print obj.innertext
Next
Write the code above on the place of dd = IE.Document.getElementsByClassName(" ")(0).innerText and see the innertext of all tr on the site.
I am trying to pull information from a website, but cannot for the life of me figure out how to click the button. I've got the code to input the information. This is the html for the website . Any help would be appreciated.
<p role="button" tabindex="0" class="fmtbutton" onkeypress="finddistancebetweenaandb(document.forms['inp']['pointa'].value,document.forms['inp']['pointb'].value);" onclick="finddistancebetweenaandb(document.forms['inp']['pointa'].value,document.forms['inp']['pointb'].value);"> Show </p>
Here is the full code that I have so far.
Sub RoundedRectangle1_Click()
Dim eRow As Long
Dim ele As Object
Dim objIE As Object
Set objIE = CreateObject("InternetExplorer.Application")
FranchiseAddress = Range("B2").Value
Movefrom = Range("B3").Value
moveto = Range("B4").Value
With objIE
.Visible = True
.navigate "https://www.freemaptools.com/how-far-is-it-between.htm"
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
Set d = .Document.getElementsByName("onoffswitch")
d.Item(0).Click
Set f = .Document.getElementsByName("pointa")
f.Item(0).Value = FranchiseAddress
Set a = .Document.getElementsByName("pointb")
a.Item(0).Value = Movefrom
' This is where I'm Stuck'
End With
End Sub
I have tried multiple solutions found on this site for many questions, but none of them seem to work.
Thank you.
add this to your code
Dim aaa As Object
Set aaa = .Document.getElementsByClassName("fmtbutton") ' this returns 4 objects, your button is the first one
aaa(0).Click
You can be a little more elegant and target the attribute with a CSS selector:
.document.querySelector("[role=button]").Click
The attribute = value selector, [role=button], looks for an element with attribute role whose value is button. There is only one on the page so no loop needed.
I am trying to use excel to scrap some information from a website.
This is what shows on source:
<tr class="even">
<td align="right">1</td>
<td>Acrobatic Maneuver</td>
<td>Instant</td>
<td>2W</td>
<td>Common</td>
<td>Winona Nelson</td>
<td><img src="http://magiccards.info/images/en.gif" alt="English" width="16" height="11" class="flag2"> Kaladesh</td>
</tr>
So I want to get everything that has even, and extract the data between the <td></td>
However, all I've found until now is this code
Sub getcards()
Dim IE As Object
Dim i As Long
Dim objCollection As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = False
' URL to get data from
IE.Navigate "http://magiccards.info/query?q=%2B%2Be%3Akld/en&v=list&s=issue"
' Statusbar
Application.StatusBar = "Loading, Please wait..."
' Wait while IE loading...
Do While IE.Busy
DoEvents
Application.Wait DateAdd("s", 1, Now)
Loop
On Error GoTo abort
Application.StatusBar = "Searching for value. Please wait..."
Dim dd As String
Set objCollection = IE.document.getElementsByClassName("even")
For i = 0 To objCollection.Length
dd = IE.document.getElementsByClassName("even")(i).innerText
MsgBox dd
Next i
abort:
' Show IE
IE.Visible = True
IE.Quit
' Clean up
Set IE = Nothing
Application.StatusBar = ""
End Sub
It works in such a way that it extracts the data, but the output is 1Acrobatic ManeuverInstant2WCommonWinona Nelson Kaladesh all together.
How can I do so it understands each <td> as a separate field, so I can extract it easily?
When you're looping through i within objCollection you're actually looping through all elements with the ClassName of "even" as opposed to the elements inside the specific element you want.
Try this:
For i = 0 To objCollection.Length - 1
For c = 0 to IE.document.getElementsByClassName("even")(i).getElementsByTagName("td").Length - 1
dd = IE.document.getElementsByClassName("even")(i).getElementsByTagName("td")(c).innerText
MsgBox dd
Next c
Next i
I'm trying to get a program to access a website, type in a zip code, add the zip code then press a button to get to the next page. My code is below.
Now I'm stuck on this page and can't get the program to click on the "Property" tab and go to the next page I want to see. Can I get any help on clicking on this button?
I've tried .GetElementsbyID("").click but that doesn't seem to work...
Web page code around the button I want to press:
<span class="geographymap-span-tab" id="tab_PROPERTY_PAGE" onclick="showSearchTypeSection('PROPERTY_PAGE','PROPERTY')"><img id="PROPERTY_PAGE_IMG" src="/list/images/PROPERTY_PAGE_2.gif" alt="property" width="80" height="29" border="0" onmouseover="MM_swapImage('PROPERTY_PAGE')" onmouseout="MM_swapImgRestore('PROPERTY_PAGE')"></span>
My code thus far is below:
Sub TestProgram()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = True
' Send the form data To URL As POST binary request
IE.Navigate "http://www.listsource.com/build.marketing.list"
' Statusbar
Application.StatusBar = "www.listsource.com is loading. Thanks and Gig 'em..."
' Application.StatusBar = False
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Focus on the drop down menu
IE.document.getElementByID("locator").Focus
'Select zip code which happens to be the 19th item
IE.document.getElementByID("locator").selectedIndex = 19
'Get to the right page based on that selection
IE.document.getElementByID("locator").FireEvent ("onchange")
'input zipcode
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Application.StatusBar = "Search form submission. Please wait..."
Set objCollection = IE.document.getElementsByTagName("textarea")
i = 0
While i < objCollection.Length
If objCollection(i).Name = "zipTextArea" Then
' Set text for search
objCollection(i).Value = "75225"
Else
If objCollection(i).Type = "button" And _
objCollection(i).Name = "addZip" Then
' "Search" button is found
Set objElement = objCollection(i)
End If
End If
i = i + 1
Wend
' pull all elements that are buttons
Set objInputs = IE.document.getElementsByTagName("button")
'click button
For Each ele In objInputs
If ele.Name Like "addZip" Then
ele.Click
End If
Next
' Wait while IE re-loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
' FIND OUT total # of SFR in each given zip code
' Click on "Property" button
End Sub
What can I add to click on the Property Tab?
Or you could check the form (searchform) for all elements named "addzip" and click on the first it encounters.
'(Dim frm as object)
'(Dim btnAdd as object)
Set frm = IE.document.forms("searchform")
Set btnAdd = frm.all("addzip")(0)
btnAdd.click
I have written a macro that will open a webpage, find the spots I need to put the data into, and then I want the macro to hit a prefill button, then hit Ok.
The page source code for the button is:
<input type="text" size="30" maxlength="40" name="name" id="name">
<input type="button" value="Prefill" onclick="prefill()">
I've been searching for answers all week and I think I have a basic understanding of how this is supposed to work by running a loop to search for it, but I'm having no luck in my endeavor of actually getting this to work.
Can someone show me the loop that will search my page for this button?
Thank you in advance.
Requested code so far
Private Sub Populate_Click()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "website" 'make sure you've logged into the page
Do
DoEvents
Loop Until IE.READYSTATE = 3
Do
DoEvents
Loop Until IE.READYSTATE = 4
ActiveSheet.EnableCalculation = False
ActiveSheet.EnableCalculation = True
Call IE.document.getelementbyid("name").SetAttribute("value", ActiveSheet.Range("b2").Value)
Call IE.document.getelementbyid("aw_login").SetAttribute("value", ActiveSheet.Range("a2").Value)
Set objCollection = IE.document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
If objCollection(i).Type = "button" And _
objCollection(i).Name = "Prefill" Then
Set objElement = objCollection(i)
End If
i = i + 1
Wend
objElement.Click
End Sub
Looks like you are pretty close, this is what I have used to do something similiar
With ie.document
Set elems = .getelementsbytagname("input")
For Each e In elems
If (e.getattribute("className") = "saveComment") Then
e.Click
Exit For
End If
Next e
End With
You will probably just have to change the if statement.
I also notice that your code refers to .Name = "Prefill" but your html snippet refers to .value = "Prefill"