0

I am trying to scrap some data from a webpage into Excel using vba.

The html code is

<span id="lastPrice">300.21</span>

I want the number 300.21

I tried this but didn't work (returned nothing in st)

Dim st As String

st = htmldoc.getElementById("lastPrice").getElementsByTagName("span")(5).innerText

How can I get the desired output ?

10
  • Do you have multiple elements that use "lastPrice" as their ID? Commented Jul 6, 2017 at 18:17
  • No I don't have.Here's the link nseindia.com/live_market/dynaContent/live_watch/get_quote/… Commented Jul 6, 2017 at 18:34
  • document.getElementById("lastPrice").innerText returns the correct number using the Chrome's Console. Could you post more code or a download link? Commented Jul 6, 2017 at 18:49
  • 2
    No worries. Please accept Kostas K. answer and close the question. Commented Jul 6, 2017 at 18:53
  • 1
    @user3126632 your question contains exactly nothing that writes to any cell, and isn't about writing to a cell. If you have another question, ask another question. Give Kostas' answer the checkmark it deserves. Commented Jul 6, 2017 at 19:25

1 Answer 1

6

Try this:

Dim element as Object
Set element = htmldoc.getElementById("lastPrice")

Dim price as String
If Not element Is Nothing Then price = element.innerText
Sign up to request clarification or add additional context in comments.

3 Comments

Where is this coming from? Is there an element showing 2200.00% anywhere on the page?
No I didn't find anything on the page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.