fizzydrink
New Member
- Joined
- Jul 25, 2023
- Messages
- 5
- Office Version
- 2019
- 2016
- Platform
- Windows
Hi,
I have seen this question googled a few times, but just can’t quite seem to get it.
I have been copying pasting and reading way too many websites. I can read and follow along vba, writing however is completely not me.
I am trying to scrape several hundred cage codes from the site:
https://cage.dla.mil/Search/
(You must be logged in and have it open first to work)
I want it to search my spreadsheet for my manufacturer number, in this case “Z06P7” from cell J1 in Sheet1
Then place that number in the search bar, submit, it and display the results.
I can do all this fine.
The part I have issue with is returning the result to Excel.
At the minimum, I am trying to get the company name to return to K1.
What I’d really like is to follow the “Details” link and then get the Company name and Phone number.
Then loop to the next row down and do again with “ZD484” etc until all (approx. 600) are complete.
I have also tried the site CAGE code lookup as this does the same thing, but having same sort of issues in returning the correct text.
I have tried element class/tags/id (‘a’. ‘tr’td’ but cant seem to draw the correct info
Any tips are appreciated.
My current code is:
Sub searchcage()
Dim ws As Worksheet
Dim targetrange As Range
Dim cell As Range
Dim copieddata As String
Dim pasteddata As String
Dim ie As Object
Dim webpage As HTMLDocument
Dim table_data As Object
Set ws = Worksheets("sheet1") ' replace with sheet name "supply support table"
Set targetrange = ws.Range("j2:j" & ws.Cells(ws.Rows.Count, "j").End(xlUp).Row) ' define search range
Set ie = CreateObject("internetexplorer.application") ' set internet explorer as ie
For Each cell In targetrange
copieddata = "Z06P7" 'copieddata = cell.Value
'open website
ie.navigate "https://cage.dla.mil/Search/" 'https://www.cage-codes.com”
ie.Visible = True
'wait for website to load
Do Until ie.readystate = 4
DoEvents
Loop
'enter copied data into search bar
ie.document.getElementById("SearchString").Value = copieddata '"SearchString" '"searchbox_id" for cage-codes
ie.document.forms(0).submit
'wait results to load
Do Until ie.readystate = 4
DoEvents
Loop
'copy the result from webpage
Set webpage = ie.document
Set table_data = webpage.getElementByTagName("Details") '("sortedby") Set table_data = webpage.getElementsByClassName("sortedby")
'paste result
ws.Range("K1").Value = table_data
'clean up
ie.Quit
Set ie = Nothing
Next
MsgBox "complete"
End Sub
I have seen this question googled a few times, but just can’t quite seem to get it.
I have been copying pasting and reading way too many websites. I can read and follow along vba, writing however is completely not me.
I am trying to scrape several hundred cage codes from the site:
https://cage.dla.mil/Search/
(You must be logged in and have it open first to work)
I want it to search my spreadsheet for my manufacturer number, in this case “Z06P7” from cell J1 in Sheet1
Then place that number in the search bar, submit, it and display the results.
I can do all this fine.
The part I have issue with is returning the result to Excel.
At the minimum, I am trying to get the company name to return to K1.
What I’d really like is to follow the “Details” link and then get the Company name and Phone number.
Then loop to the next row down and do again with “ZD484” etc until all (approx. 600) are complete.
I have also tried the site CAGE code lookup as this does the same thing, but having same sort of issues in returning the correct text.
I have tried element class/tags/id (‘a’. ‘tr’td’ but cant seem to draw the correct info
Any tips are appreciated.
My current code is:
Sub searchcage()
Dim ws As Worksheet
Dim targetrange As Range
Dim cell As Range
Dim copieddata As String
Dim pasteddata As String
Dim ie As Object
Dim webpage As HTMLDocument
Dim table_data As Object
Set ws = Worksheets("sheet1") ' replace with sheet name "supply support table"
Set targetrange = ws.Range("j2:j" & ws.Cells(ws.Rows.Count, "j").End(xlUp).Row) ' define search range
Set ie = CreateObject("internetexplorer.application") ' set internet explorer as ie
For Each cell In targetrange
copieddata = "Z06P7" 'copieddata = cell.Value
'open website
ie.navigate "https://cage.dla.mil/Search/" 'https://www.cage-codes.com”
ie.Visible = True
'wait for website to load
Do Until ie.readystate = 4
DoEvents
Loop
'enter copied data into search bar
ie.document.getElementById("SearchString").Value = copieddata '"SearchString" '"searchbox_id" for cage-codes
ie.document.forms(0).submit
'wait results to load
Do Until ie.readystate = 4
DoEvents
Loop
'copy the result from webpage
Set webpage = ie.document
Set table_data = webpage.getElementByTagName("Details") '("sortedby") Set table_data = webpage.getElementsByClassName("sortedby")
'paste result
ws.Range("K1").Value = table_data
'clean up
ie.Quit
Set ie = Nothing
Next
MsgBox "complete"
End Sub