VBA Getting Elements By ClassName

cstewart28

New Member
Joined
May 4, 2016
Messages
7
I trying to get a list the of items based on the class name list-group-item on this page (https://www.cattle.com/markets/archive.aspx?code=TV_LS153) and then populate them across column fields and I need the dates to stay in the same form as yyyy-mm-yy format. My spreadsheet in column A hold the TV_LS153 information.

I keep getting an error at line "Sheets("Sheet2").Range("B" & y).Value = element.innerText"

Sub GetClassNames()

'dimension (declare or set aside memory for) our variables
Dim HTML As HTMLDocument

Dim objIE As Object

Dim y As Integer 'integer variable we'll use as a counter

Dim result As String 'string variable that will hold our result link

'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer

'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True

'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://www.cattle.com/markets/archive.aspx?code=" & Range("A1").Text

'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

y = b

Set HTML = objIE.document

Set elements = HTML.getElementsByClassName("list-group-item")

For Each element In elements
If element.className = "list-group-item" Then
Sheets("Sheet2").Range("B" & y).Value = element.innerText
y = y + 1
End If

Next element

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Run-time error '1004'

Application-defined or object-defined error

When I click debug it stops at 'Sheets("Sheet2").Range("B" & y).Value = element.innerText"
 
Upvote 0
That's probably because, as far as I can see, the value of y is 0.

This is where you set the value of y,
Code:
y = b
but you aren't setting the value of b anywhere in the code.
 
Upvote 0
ugh, I set it y + 1 and now it works for the most part. The issue not is that information is going down the rows and not across in columns
 
Upvote 0
To go across columns try this.
Code:
For Each element In elements
    If element.className = "list-group-item" Then
        Sheets("Sheet2").Cells(1, y).Value = element.innerText ' change 1 to the row you want the data to go to
        y = y + 1
    End If
Next element
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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