4

Am stuck with this,

VBA code:

Set html = IE.document

Set elements = html.getElementsByClassName("dropdown dropdown-toggle editable-field-title-wrapper")

Dim count As Long
Dim erow As Long
count = 0
For Each element In elements
If element.className = "dropdown dropdown-toggle editable-field-title- wrapper" Then
erow = Sheets("Exec").Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Sheets("Exec").Cells(erow, 1) = html.getElementsByTagName("span") 
(count).innerText

count = count + 1
End If
Next element

HTML code:

<span data-link="class{:~getPriorityLabel(fieldValue) ? 'editable-field label-priority-color-' + ~getPriorityLabel(fieldValue) : 'editable-field'}" class="editable-field label-priority-color-high">
    <span class="dropdown dropdown-toggle editable-field-title-wrapper" data-toggle="dropdown">
      <script type="jsv#432^"></script><script type="jsv#814_"></script>
      <span class="editable-field-title">Priority:</span>
      <script type="jsv/814_"></script><script type="jsv/432^"></script>
      <span data-link="html{>~isUndefined(fieldValue) ? 'None' : ~capitalize(~getPriorityLabel(fieldValue))}">**High**</span>
      <span data-link="visible{:state != 'saving'}" style="display: block;">
        <span class="caret"></span>
      </span>
      <span data-link="visible{:state == 'saving'}" style="display: none;">
        <script type="jsv#815_"></script><i class="icon-spinner"></i><script type="jsv/815_"></script>
      </span>
    </span>
    <ul class="dropdown-menu priority-dropdown pull-right"><li data-link="visible{:fieldValue}" style="display: list-item;">
        <a class="editable-field-commit" data-value="-1" href="#" tabindex="0">None</a>
      </li><li>
        <a class="editable-field-commit" data-value="0.07438533240156266" href="#" tabindex="0">Low</a>
      </li><li>
        <a class="editable-field-commit" data-value="0.49243981401033865" href="#" tabindex="0">Medium</a>
      </li><li>
        <a class="editable-field-commit" data-value="0.9642772538736262" href="#" tabindex="0">High</a>
      </li></ul>
    <div class="alert alert-error editable-field-message-open-left editable-field-error-message" data-link="html{>errorMessage} visible{:errorMessage}" style="display: none;"></div>
    <div class="alert alert-info editable-field-conflict-message" data-link="html{>conflictMessage} visible{:conflictMessage}" style="display: none;"></div>
  </span>

Please find the High in my HTML code and I want that letter(High) using my Excel VBA code, please help me out.!!

Please find the High in my HTML code and I want that letter(High) using my Excel VBA code, please help me out.!!

1
  • Can you provide more of the html or an url?
    – QHarr
    Jul 26, 2018 at 4:35

2 Answers 2

1

If you actually mean the "High" then you can use a CSS selector.

Debug.Print html.querySelector("span[data-toggle=dropdown]").innerText

CSS query using selector:

css selector

This applies a CSS selector of span[data-toggle=dropdown], which is element(s) with span tag having attribute data-toggle with value of dropdown. I have used querySelector, which only returns the first match, as your HTML shown only has one matching element.

If you have more than one matching element then you will need the querySelectorAll method, which returns a nodeList of matching elements, and then index into that 0-based nodeList using the appropriate index e.g.

html.querySelectorAll("span[data-toggle=dropdown]").item(0).innerText

You would traverse the .Length of the nodeList to get all items e.g.

Dim aNodeList As Object, i As Long, erow As Long, count As Long
Set aNodeList  =  html.querySelectorAll("span[data-toggle=dropdown]")
For i = 0 To aNodeList.Length -1
    erow = Sheets("Exec").Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
    Sheets("Exec").Cells(erow, 1)  =  aNodeList.item(i).innerText
    count = count + 1
Next i
9
  • here, Set html = IE.document Set elements = html.getElementsByClassName("dropdown dropdown-toggle editable-field-title-wrapper") Dim count As Long Dim erow As Long count = 0 For Each element In elements If element.className = "dropdown dropdown-toggle editable-field-title- wrapper" Then erow = Sheets("Exec").Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row Sheets("Exec").Cells(erow, 1) = html.getElementsByTagName("span") (count).innerText count = count + 1 End If Next element Where should I placeyour code?
    – iltech
    Jul 26, 2018 at 10:10
  • I have. Look at the very bottom. :-) My code replaces that. You said you wanted the text "High". You should only need html.querySelector("span[data-toggle=dropdown]").innerText for that if first element. Otherwise, you use the very bottom one to get all matched elements.
    – QHarr
    Jul 26, 2018 at 10:11
  • is the URL internal?
    – QHarr
    Jul 26, 2018 at 10:15
  • u mean. Debug.Print html.querySelector("span[data-toggle=dropdown]").innerText should be replaced Sheets("Exec").Cells(erow, 1) = element.getElementsByTagName("span")(count).innerHTML here?
    – iltech
    Jul 26, 2018 at 10:16
  • Is there a single value to retrieve or 1? If 1 then simply say Sheets("Exec").Cells(1,1) = html.querySelector("span[data-toggle=dropdown]").innerText . No loop. Change the row number to the required row.
    – QHarr
    Jul 26, 2018 at 10:18
0

Using the traditional way.

Change inside for loop ===>
html.getElementsByTagName("span")(count).innerText To
element.getElementsByTagName("span")(count).innerText

Set html = IE.document

Set elements = html.getElementsByClassName("dropdown dropdown-toggle editable-field-title-wrapper")

Dim count As Long
Dim erow As Long
count = 0
For Each element In elements
If element.className = "dropdown dropdown-toggle editable-field-title- wrapper" Then
erow = Sheets("Exec").Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Sheets("Exec").Cells(erow, 1) = html.getElementsByTagName("span")(count).innerText '--> change here

count = count + 1
End If
Next element
3
  • Just getting blank cell as a result
    – iltech
    Jul 26, 2018 at 10:51
  • btw am getting Object variable or with block variable not set as a error pop up if I replace the code as you mentioned in your answer
    – iltech
    Jul 26, 2018 at 10:53
  • Using watch window, see whats in element and then element.getElementsByTagName("span")
    – Santosh
    Jul 26, 2018 at 10:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.