Hi All,
I'm new to this forum but found it very useful whenever I've had a problem with Excel or VBA. But get to the point. I'am currently working on a macro that will enable me to parse few strings from an online HTML source (ie. webpage) and write these values to an excel using VBA. So far I've come up with the code presented below, however there are few things:
1. I don't know how to tell excel to write each found string to one cell and then move to the next cell to write the next string. (it is a loop within a loop within a loop)
2. The data I'm interested in occurs several times within HTML code always between two another strings (I've managed to build a very naive loop to find all (or at least most) of the data occurences I'm interested in and show me each occurence in a message box). I'd like to put these values in excel's cells rather than see them in msgbox.
3. Maybe You know some better, more robust, way to find every occurence of a string within a string and paste each time it is found to a different cell?
The code is as follows:
I'm new to this forum but found it very useful whenever I've had a problem with Excel or VBA. But get to the point. I'am currently working on a macro that will enable me to parse few strings from an online HTML source (ie. webpage) and write these values to an excel using VBA. So far I've come up with the code presented below, however there are few things:
1. I don't know how to tell excel to write each found string to one cell and then move to the next cell to write the next string. (it is a loop within a loop within a loop)
2. The data I'm interested in occurs several times within HTML code always between two another strings (I've managed to build a very naive loop to find all (or at least most) of the data occurences I'm interested in and show me each occurence in a message box). I'd like to put these values in excel's cells rather than see them in msgbox.
3. Maybe You know some better, more robust, way to find every occurence of a string within a string and paste each time it is found to a different cell?
The code is as follows:
Code:
Sub GatherData()
'Gather data from HTML code
'References to enable: Microsoft Internet Controls, OLE Automation, Microsoft Excel 'apppropriate ver.' Object Library
Dim IE As Object
Dim x As String
Dim z As String
Dim lTemp As Long
Dim xtemp As String
Set IE = CreateObject("InternetExplorer.Application") 'Load IE
IE.Visible = True 'Like to see how everything happens, as well might be turned to False
For Mystr = 2 To 3 'To loop through subpages
IE.navigate "http://www.webpage.com/search?page=" & Mystr
' Let IE load everything on the page
Do Until IE.readyState = READYSTATE_COMPLETE
DoEvents
Loop
z = "tr id=" 'the string after which the interesting part of data is within HTML code
x = IE.Document.Body.InnerHTML 'Use HTML code as a string
k = InStr(x, z) 'The position of the first occurence of "tr id=" string within the HTML code
w = Len(x) 'Length of HTML code (string)
'There are many different occurences of the data i'm looking for,
'Below is a very simplistic approach to extract most(due to the fixed step - I don't know how to apply variable(?))
'of the occurences. The idea is to find the position of the first ocurence of the data ('k' above)
'and then setting a constant distance to the next occurence (in terms of number of signs).
'This number is subtracted for each iteration
'(to shorten the string, i.e. exclude the first occurence to find the next, 'xtemp' below).
'The GetBetween function is meant to extract text between "tr id=" and "class" string from the xtemp string.
'As for now the data I'm interested in pops-up in a message box, but I'd like to write each occurence to excel cell,
'then move down one cell and write the next occurence
'- all the way down until all occurences on all subpages are in one column.
For y = k To w Step 3000
lTemp = w - y
xtemp = Right(x, lTemp)
occur = GetBetween(xtemp, "tr id=", "class")
MsgBox occur
Next y
Next Mystr
End Sub