I need to have VBA look into the HTML of a website, find a certain string within the text, and place into an Excel cell a value containing that string, and X characters to the left of that string, let's say 20 for the sake of example.
For example, if I need to find the string "elit" in a site containing the following string:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
The code would need to return a value of "sectetur adipiscing elit" to a specified cell. That is, the string itself, and 20 characters to the left of the string.
Here's what I've come up with so far (I know .select is not best practice but it works for me):
Sub String_Checker()
Sheets("Sheet1").Range("a2").Select
Dim IE As Object
Do Until IsEmpty(ActiveCell)
Set IE = CreateObject("internetexplorer.Application")
IE.Visible = True
IE.navigate "https://website.com"
Do Until (IE.readyState = 4 And Not IE.Busy)
DoEvents
Loop
Set objDoc = IE.document
strMyPage = objDoc.body.innerHTML
Dim s As String: s = ActiveCell.Offset(0, 1).Value
ActiveCell.Offset(0, 2).Value = Left(strMyPage, 20)
IE.Quit
ActiveCell.Offset(1, 0).Select
Loop
End Sub
That gives me the last 20 characters of the HTML, but I need to get the code to start "looking" at the specified string, which would consistently be ActiveCell.Offset(0,1).Value in the Excel. Any help would be appreciated. Thanks!
innertextinstead ofinnerhtml