• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Trouble understanding the difference between two conditional statements

shahin

Active Member
I've written some code to experiment how conditional statement works when I apply it on a webpage to parse a certain item from. I used if statement in two different ways in my script. The one brings messy text is commented out in my script. If more accurately described then the commented out "If statement with Instr() function" is fetching a whole bunch of junk along with the one I'm after. However, the uncommented if statement is working just fine but it is seriously fragile because when my search becomes "R016698 " (mark the ending space) instead of "R016698" It fails to catch that. Why my commented out "If statement" is not working as I expected (I meant to get the exact value and nothing else) and how to fix that? I hope I could make my description clear. Any help would be much appreciated.

Here is the code:

Code:
Sub Get_Page()
    Dim IE As New InternetExplorer, html As HTMLDocument, post As Object

    With IE
        .Visible = True
        .navigate "http://search.wcad.org/Property-Search-Result?searchtext=319%20lizzie%20taylor"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = .document
    End With

    Application.Wait Now + TimeValue("0:00:05")
    For Each post In html.getElementsByTagName("td")
        ''If InStr(post.innerText, "R016698") > 0 Then [A1] = post.innerText: Exit For
        If post.innerText = "R016698" Then [A1] = post.innerText: Exit For
    Next post
    IE.Quit
End Sub

The result I get using uncommented "If statement":

Code:
R016698

The result I get using commented out "If statement with Instr() function" is huge which I have attached in a text file below.
 

Attachments

Last edited:
It is working as expected

If you uncomment the commented If statement
then put a break-point on that line "F9"
Then run the code
When the code stops in the Immediate window type:
? mid(post.innertext, InStr(post.innerText, "R016698"), 7)
it returns R016698

If you type
? InStr(post.innerText, "R016698")
it returns 596

So the 596-602 characters are: R016698

Here is your text file: upload_2017-12-26_10-5-55.png

Instr is simply telling you what position the text is in the string
as it is 596 it is > 0 and so it is true
and behaves as expected
 
Thanks sir Hui for your descriptive answer. If you run the below script, you will notice that it parses only the item I mention in the Instr() function irrespective of it's position and without bringing together unwanted items that I didn't search for.

Code:
Sub Get_Page()
    Dim IE As New InternetExplorer, html As HTMLDocument, post As Object

    With IE
        .Visible = True
        .navigate "https://chandoo.org/forum/forums/vba-macros/"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = .document
    End With

    Application.Wait (Now + TimeValue("0:00:05"))
    For Each post In html.getElementsByTagName("a")
        If InStr(post.innerText, "VDI") > 0 Then Row = Row + 1: Cells(Row, 1) = post.innerText: Exit For
    Next post
    IE.Quit
End Sub

Output:

Code:
Excel 2013 macro working erratically in AppSphere (VDI) platform running Server 2008

That is why I get confused about the activity of the instr() function. Because you can see the variation between what the function did in the first script and what is it doing now.
 
Last edited:

Hi !

Just read VBA inner help or again Hui's answer …
You just forget to parse in your code !
 
If you just want the VDI if VDI exists in the text
why not
Code:
If InStr(post.innerText, "VDI") > 0 Then Row = Row + 1: Cells(Row, 1) = "VDI": Exit For
 
@sir Hui, I'm sorry that I could not make things clear. My "post 2" is an example of how I expected my "post 1" should do. I've got no issues with "post 2". It is doing just fine.
 
let me elaborate

InStr("abcVDIdef", "VDI") > 0
will return true because VDI is in position 4 in "abcVDIdef"
4 > 0

so
Cells(Row, 1) = "abcVDIdef"
is what it is doing

In the first example "R016698" is in position 596
So it returns the string post.innerText
which is a few thousand characters long

If you just want the text why not use
If post.innerText = "R016698" Then [A1] = "R016698": Exit For
 
Back
Top