Hi, I have a have been using VBA for under a year, never posted anything, but I am having a lot of trouble with my function being non-deterministic. I need to check web pages, look through their links (put them into an array), and check if another webpage is one of those links. Here is the important part of my code as it stands
Private Sub TestLink(ColumnNumSite As Integer, RowNum As Integer, ColumnNum As Integer)
Dim lnk
Dim MyNames() As String ' declares a dynamic array variable
Dim iCount As Integer
Dim Max As Integer
Dim ie As InternetExplorer
Dim lngLoc As Long
Set ie = CreateObject("InternetExplorer.Application")
With ie
'.Visible = True
.Navigate Cells(RowNum, ColumnNumSite)
Do Until .ReadyState = 4: DoEvents: Loop
Set doc = ie.Document
Max = doc.Links.length
ReDim MyNames(1 To Max + 2)
iCount = 1
For Each lnk In doc.Links
' finds the maximum array size
'For iCount = 1 To Max
MyNames(iCount) = lnk
'ActiveSheet.Cells(RowNum + iCount - 1, 6 + iCount) = lnk
iCount = iCount + 1
'MyNames(iCount) = ThisWorkbook.Names(iCount)
Next
lngLoc = -9
On Error Resume Next
lngLoc = Application.WorksheetFunction.Match(ActiveSheet.Cells(RowNum, ColumnNum).Formula(), MyNames(), 0)
ActiveSheet.Cells(RowNum, ColumnNumSite + 5).Value() = lngLoc
If (lngLoc = -9) Then
ActiveSheet.Cells(RowNum, ColumnNumSite + 4).Value() = "Link Does not Exist"
Else: ActiveSheet.Cells(RowNum, ColumnNumSite + 4).Value() = "Link Exists"
End If
.Quit
End With
End Sub
Right now whats happening is max gets set to a very small value ocassionally (around 3 or 28), but if I pause it and rerun that line it will recieve its full normal value. This is not the entire problem because I tried to
ReDim MyNames(1 To 1000)
instead of to Max, and it broke anyways, the breaking occurs because
For Each lnk In doc.Links
does not go through all the links sometimes, which I know because when I pause it afterwards and rerun that area I get the correct solution. Does anyone know whats going on? All I need is to set an array to the links of a webpage
Private Sub TestLink(ColumnNumSite As Integer, RowNum As Integer, ColumnNum As Integer)
Dim lnk
Dim MyNames() As String ' declares a dynamic array variable
Dim iCount As Integer
Dim Max As Integer
Dim ie As InternetExplorer
Dim lngLoc As Long
Set ie = CreateObject("InternetExplorer.Application")
With ie
'.Visible = True
.Navigate Cells(RowNum, ColumnNumSite)
Do Until .ReadyState = 4: DoEvents: Loop
Set doc = ie.Document
Max = doc.Links.length
ReDim MyNames(1 To Max + 2)
iCount = 1
For Each lnk In doc.Links
' finds the maximum array size
'For iCount = 1 To Max
MyNames(iCount) = lnk
'ActiveSheet.Cells(RowNum + iCount - 1, 6 + iCount) = lnk
iCount = iCount + 1
'MyNames(iCount) = ThisWorkbook.Names(iCount)
Next
lngLoc = -9
On Error Resume Next
lngLoc = Application.WorksheetFunction.Match(ActiveSheet.Cells(RowNum, ColumnNum).Formula(), MyNames(), 0)
ActiveSheet.Cells(RowNum, ColumnNumSite + 5).Value() = lngLoc
If (lngLoc = -9) Then
ActiveSheet.Cells(RowNum, ColumnNumSite + 4).Value() = "Link Does not Exist"
Else: ActiveSheet.Cells(RowNum, ColumnNumSite + 4).Value() = "Link Exists"
End If
.Quit
End With
End Sub
Right now whats happening is max gets set to a very small value ocassionally (around 3 or 28), but if I pause it and rerun that line it will recieve its full normal value. This is not the entire problem because I tried to
ReDim MyNames(1 To 1000)
instead of to Max, and it broke anyways, the breaking occurs because
For Each lnk In doc.Links
does not go through all the links sometimes, which I know because when I pause it afterwards and rerun that area I get the correct solution. Does anyone know whats going on? All I need is to set an array to the links of a webpage