VBA Excel getElementsByClassName without child results

yamik85

New Member
Joined
Feb 12, 2016
Messages
2
Hi

I have problem with "getElementsByClassName". I am trying to get only the FIRST one class without any child.
I have: Set elements = html.getElementsByClassName("typstable")
But in the results, I got also "typstable tabMundial" what is no needed. How can I take only the first one result or exactly the name of the class without any child?

I don't know exactly quantity of records for the tables what I want. Tables have random records numbers. I can check the data in the excel if the entries is correct and delete the wrong ones but I hope there is a better solution like "html.getElementsByClassName("typstable")[1] or html.getElementsByClassName("typstable")(1)" I don't know. But this solutions also makes the problem if class with child will be first so the best solution will be take only the class tag without any childes.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
getElementsByClassName returns a collection of elements, and the index for the collection starts at 0. So to return the first element in the collection...

Code:
html.getElementsByClassName("typstable")[COLOR=#ff0000](0)[/COLOR]

So if you were to use early binding, you would do this way...

Code:
Dim element As MSHTML.IHTMLElement

Set element = HTML.getElementsByClassName("typstable")(0)

Hope this helps!
 
Upvote 0
Thx for the answer. I was trying before:
Code:
Set element = HTML.getElementsByClassName("typstable")(0)

And it always gave me an error so I thought it's impossible to do like that. Now I did step by step again and I found my silly fault:)
I got collection..
Code:
Dim elements As IHTMLElementCollection
Set elements = html.getElementsByClassName("typstable")(0)
Now I did like you wrote:
Code:
Dim element2 As IHTMLElement
Set element2 = html.getElementsByClassName("typstable")(0)

And it works like I want now. Thx for the help.
 
Upvote 0

Forum statistics

Threads
1,215,331
Messages
6,124,311
Members
449,152
Latest member
PressEscape

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