Sharid
Well-known Member
- Joined
- Apr 22, 2007
- Messages
- 1,064
- Office Version
- 2016
- Platform
- Windows
I use the following code to navigate pages on the web, it works fine, however it needs updating. I can put in the the number of pages to navigate in Sheet1 C1 and 2x delays in D1 and E1. I place the item to search in Sheet1 B1.
I would now like to have a list of items in Sheet2 column A. I would like the code below to search each item in the column for the number of pages input into Sheet1 C1. Currently I can ONLY do one item, then the code stops. I then have to add a new item and run it again. I would prefer to add a list of items in sheet2 column A and the code automates the changes by itself after it has reached the max page number or there is nothing more for that item to extract.
Currently it will exit to Do if there are no more items to extract, at this point it should move to the next item in sheet 2 or when X amount of pages has been achived (Page number to navigate is in Sheet1 C1)
This part of the code is the issue, works fine just needs updating.
I would now like to have a list of items in Sheet2 column A. I would like the code below to search each item in the column for the number of pages input into Sheet1 C1. Currently I can ONLY do one item, then the code stops. I then have to add a new item and run it again. I would prefer to add a list of items in sheet2 column A and the code automates the changes by itself after it has reached the max page number or there is nothing more for that item to extract.
Currently it will exit to Do if there are no more items to extract, at this point it should move to the next item in sheet 2 or when X amount of pages has been achived (Page number to navigate is in Sheet1 C1)
This part of the code is the issue, works fine just needs updating.
VBA Code:
'''Searches Number of Pages entered in Sheet1 C1
If pageNumber >= Replace(Worksheets("Sheet1").Range("C1").Value, " ", "+") Then Exit Do
On Error Resume Next
Set nextPageElement = htmlDoc.getElementById("pnnext")
''' Exit DO if items pages are less than pages entered, after last page is done
If nextPageElement Is Nothing Then Exit Do
'''Scrolls Down the Browser
IE.document.parentWindow.Scroll 0&, 99999
'''Random delay from Max number entered in Sheet1 D1
Application.Wait Now + TimeSerial(0, 0, Application.RandBetween(1, Worksheets("Sheet1").Range("D1").Value))
'''next web page
nextPageElement.Click
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
'''Random delay from Max number entered in Sheet1 E1
Application.Wait Now + TimeSerial(0, 0, Application.RandBetween(1, Worksheets("Sheet1").Range("E1").Value))
Set htmlDoc = IE.document
pageNumber = pageNumber + 1
''' Delete duplicates
Sheet1.Columns("A").RemoveDuplicates Columns:=Array(1), Header:=xlYes
''' Delete Row If Blank
Sheet1.Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Loop
MsgBox "All Done"