• 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.

How to modify my script to make use of Like operator in the desired way?

shahin

Active Member
I've written a script in vba to get some unwanted links started with "#" using "Like" operator from a webpage. I could sift through the links If I opt for "Instr()" function. However, my intention is to grab those links using "Like" operator. How can I modify my script to get them?

Usual links look like:
Code:
javascript:document.form1.submit();

The ones I wish to grab look like:
Code:
#top

My script can grab all of the types but I wish to stick to grab only the links started with "#".

This is my attempt so far:
Code:
Sub ClickLinks()
    Const Url As String = "https://intraweb.stockton.edu/eyos/page.cfm?siteID=58&pageID=7&action=dirmain&type=FAC&display=basic"
    Dim IE As New InternetExplorer, Htmldoc As HTMLDocument, I&

    With IE
        .Visible = True
        .navigate Url
        While .Busy = True Or .readyState < 4: DoEvents: Wend
        Set Htmldoc = .document
    End With

    With Htmldoc.querySelectorAll("#main table tr a")
        For I = 0 To .Length - 1
            If .Item(I).getAttribute("href") Like "#*" Then 'can't fix this line to get the links started with #
                Cells(I + 1, 1) = .Item(I).getAttribute("href")
            End If
        Next I
    End With
    IE.Quit
End Sub
 
Hello
You can use
Code:
If .Item(I).getAttribute("href") Like "[#]" & "*" Then
Or simply
Code:
If Left(.Item(I).getAttribute("href"), 1) = "#" Then
 
This is final code
Code:
Sub ClickLinks()
    Const Url As String = "https://intraweb.stockton.edu/eyos/page.cfm?siteID=58&pageID=7&action=dirmain&type=FAC&display=basic"

    Dim ie          As New InternetExplorer
    Dim htmlDoc    As HTMLDocument
    Dim i          As Long
    Dim r          As Long

    With ie
        .Visible = True
        .navigate Url
        While .Busy = True Or .readyState < 4: DoEvents: Wend
        Set htmlDoc = .document
    End With

    With htmlDoc.querySelectorAll("#main table tr a")
        For i = 0 To .Length - 1
            If .Item(i).getAttribute("href") Like "[#]" & "*" Then
                r = r + 1
                Cells(r, 1) = .Item(i).getAttribute("href")
            End If
        Next i
    End With

    ie.Quit
End Sub
 
Thanks a lot Mr. Marc
You're right. I was interested in excluding the hash and to deal it as character and I didn't pay attention to that approach
Thanks a lot
 
Agreed. However, it seems there is a huge simililarity between the patterns of Like operator and Regex.
 
Back
Top