Solved

pulling info from a website and putting information into listbox

Posted on 2014-02-12
3
156 Views
Last Modified: 2014-02-14
excel vba

Userform1
Listbox
Commandbutton

webbrowser1 object on userform


What I need:
After I navigate to a website:
The web page displays several items on the web page.

I need to find every instance of the html code below and put certain intomation into a listbox:

That starts with this html code part:
</a></div><div class

</a></div><div class="productDescription"><p class="productName"><a href="/product/VAUGHAN-Curved-Claw-Hammer-6CLR0?s_pp=false&sgAttributes=" class="productLink" title="Curved-Claw Hammer, Steel, 20 Oz, Smooth">Curved-Claw Hammer, Steel, 20 Oz, Smooth</a></p><p class="productBrand"> VAUGHAN</p><p class="productInfo" id="itemNumber2">Grainger Item # <a href="/product/VAUGHAN-Curved-Claw-Hammer-6CLR0?s_pp=false&sgAttributes=" class="productLink" title="Curved-Claw Hammer, Steel, 20 Oz, Smooth"><span class="productInfoValueList"> 6CLR0</span></a><span class="productMFR"> | Mfr. Model # <span class="productInfoValueList"> R20</span></span></p><p id="productCatalogPage2" class="productCatalog"> Catalog Page # <a href="javascript:void(0)" onclick="detectForCatalog('1117')"><span class="productInfoValueList">1117</span></a> (PDF) </p><p class="productIcons">





So from this information in the html the information i need are:
?s_pp=false&sgAttributes=" class="productLink" title="Curved-Claw Hammer, Steel, 20 Oz, Smooth"

I need:
Curved-Claw Hammer, Steel, 20 Oz, Smooth



><span class="productInfoValueList"> 6CLR0
I need :
6CLR0


</a></p><p class="productBrand"> VAUGHAN
I need:
VAUGHN


Mfr. Model # <span class="productInfoValueList"> R20
I NEED:
R20



Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39858409
You have to find unique text that will always be before and after the items you are interested in. Then you can use this function to extract them.

Function ExtractString(strInput As String, strUniqueStart As String, strUniqueEnd As String) As String

    ExtractString = Split(Split(strInput, strUniqueStart)(1), strUniqueEnd)(0)

End Function

Open in new window


For example
Sub Demo()

    Dim strHtml As String
    Dim strUniqueStart As String
    Dim strUniqueEnd As String

    strHtml = "</a></div><div class=""productDescription""><p class=""productName""><a href=""/product/VAUGHAN-Curved-Claw-Hammer-6CLR0?s_pp=false&sgAttributes="" class=""productLink"" title=""Curved-Claw Hammer, Steel, 20 Oz, Smooth"">Curved-Claw Hammer, Steel, 20 Oz, Smooth</a></p><p class=""productBrand""> VAUGHAN</p><p class=""productInfo"" id=""itemNumber2"">Grainger Item # <a href=""/product/VAUGHAN-Curved-Claw-Hammer-6CLR0?s_pp=false&sgAttributes="" class=""productLink"" title=""Curved-Claw Hammer, Steel, 20 Oz, Smooth""><span class=""productInfoValueList""> 6CLR0</span></a><span class=""productMFR""> | Mfr. Model # <span class=""productInfoValueList""> R20</span></span></p><p id=""productCatalogPage2"" class=""productCatalog""> Catalog Page # <a href=""javascript:void(0)"" onclick=""detectForCatalog('1117')""><span class=""productInfoValueList"">1117</span></a> (PDF) </p><p class=""productIcons"">"

    ' Extract data and show messagebox (this is only for demonstration purpose, in real life you don't need it)
    strUniqueStart = "?s_pp=false&sgAttributes="" class=""productLink"" title="""
    strUniqueEnd = "</a></p><p class=""productBrand"">"
    MsgBox Split(ExtractString(strHtml, strUniqueStart, strUniqueEnd), """>")(0)

    strUniqueStart = "<span class=""productInfoValueList"">"
    strUniqueEnd = "</span></a>"
    MsgBox ExtractString(strHtml, strUniqueStart, strUniqueEnd)

    strUniqueStart = "<p class=""productBrand"">"
    strUniqueEnd = "</p><p class=""productInfo"
    MsgBox ExtractString(strHtml, strUniqueStart, strUniqueEnd)

    strUniqueStart = "Mfr. Model # <span class=""productInfoValueList"">"
    strUniqueEnd = "</span></span></p>"
    MsgBox ExtractString(strHtml, strUniqueStart, strUniqueEnd)

    ' Add items to listbox
    With ListBox1    ' the name of your listbox
        strUniqueStart = "?s_pp=false&sgAttributes="" class=""productLink"" title="""
        strUniqueEnd = "</a></p><p class=""productBrand"">"
        .AddItem Split(ExtractString(strHtml, strUniqueStart, strUniqueEnd), """>")(0)
        strUniqueStart = "<span class=""productInfoValueList"">"
        strUniqueEnd = "</span></a>"
        .AddItem ExtractString(strHtml, strUniqueStart, strUniqueEnd)
        strUniqueStart = "<p class=""productBrand"">"
        strUniqueEnd = "</p><p class=""productInfo"
        .AddItem ExtractString(strHtml, strUniqueStart, strUniqueEnd)
        strUniqueStart = "Mfr. Model # <span class=""productInfoValueList"">"
        strUniqueEnd = "</span></span></p>"
        .AddItem ExtractString(strHtml, strUniqueStart, strUniqueEnd)
    End With

End Sub

Open in new window

0
 
LVL 3

Author Comment

by:fordraiders
ID: 39859241
macroshadow, Very nicely done thanks.

Can each item i add go into a seperate column in the listbox ?
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 39860088
thanks... You answered my initial question
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now