Accessing web USING VBA to collect data

raghib

New Member
Joined
May 20, 2023
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hey folks,
I'm hoping someone can help me out with a VBA code. I need something that will open the website Heavy Duty Truck Parts Online, Medium Duty, Light Duty - FinditParts, search for the PartNo located in Column A, on the website, and provide the results in Column B. The result should be the product description.

Attaching the file to show input and output. Thank you.

Input sheet:
PartNo
ProductDescription
1492193
8891040

Output sheet:

PartNo
ProductDescription
1492193
BUYERS PRODUCTS 1492193 - 4in. Square LED Flood Light with Switch and Handle
8891040
BUYERS PRODUCTS 8891040 - 11in. Rectangular Multi-Mount Amber LED Mini Light Bar
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You can use the following function to get the product description per part number:
VBA Code:
Option Explicit

'Add references to Microsoft WinHttp Services, version 5.1 and Microsoft HTML Object Library
Public Function GetFindItPartsProductInfo(ByVal PartNumber As Variant) As String
    If IsArray(PartNumber) = False Then
        Dim objWinHttp As WinHttp.WinHttpRequest
        Set objWinHttp = New WinHttp.WinHttpRequest
        With objWinHttp
            .Open "GET", "https://www.finditparts.com/search?utf8=%E2%9C%93&s=" & CStr(PartNumber) & "&aggs=true&page=1&aggs=1&strict_phrase=1&commit=", True
            .SetRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
            .SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50"
            .Send
            .WaitForResponse
            If .Status = 200 Then
                Dim objHtmlDoc As MSHTML.HTMLDocument
                Set objHtmlDoc = New MSHTML.HTMLDocument
                objHtmlDoc.Body.innerHTML = .ResponseText
                Dim objHtmlSpanElement As MSHTML.HTMLSpanElement
                Set objHtmlSpanElement = objHtmlDoc.querySelector("[itemprop='description']")
                Dim strDescription As String
                strDescription = Trim(objHtmlSpanElement.textContent)
                Dim objHtmlManufacturerElement As MSHTML.HTMLSpanElement
                Set objHtmlManufacturerElement = objHtmlDoc.getElementsByClassName("manufacturer").Item(0)
                Dim strManufacturer As String
               strManufacturer = Trim(objHtmlManufacturerElement.textContent)
                GetFindItPartsProductInfo = strManufacturer & " " & CStr(PartNumber) & " - " & strDescription
            End If
        End With
    End If
End Function

1684578553913.png
 
Upvote 0
Thank you for your efforts however this code is not working and keeps asking macro name. Can you advise what I have been doing wrong?
 
Upvote 0
Thank you for your efforts however this code is not working and keeps asking macro name. Can you advise what I have been doing wrong?
You could download this .xlsm workbook, then check to see if the code is working.
For example, "=GetFindItPartsProductInfo(B3)".
1684773044456.png
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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