Home » excel » excel – Error while calling sub in vba

excel – Error while calling sub in vba

Posted by: admin May 14, 2020 Leave a comment

Questions:

Am stuck and not able to proceed with this. Please find my code below. The code is basically to verify if an element is present in a webpage through VBA. I have created the below sub.

Sub ele_exist(val As String, ele As String)

Select Case val:

Case "byid":
    Set verielement = doc.getElementById(ele)
    If verielement Is Nothing Then
    msgbox("something")
    Else
   msgbox("something")
    End If

Case "byclass":
    Set verielement = doc.getElementsByClassName(ele)
    If verielement Is Nothing Then
    msgbox("something")
    Else
   msgbox("something")
    End If

Case "byname":
    Set verielement = doc.getElementsByName(ele)
    If verielement Is Nothing Then
   msgbox("something")
    Else
   msgbox("something")
    End If

End Select

End Sub

Now when i call this sub it gives syntax error

This is where i call the above sub

Sub start()
Set ie = New InternetExplorer
    With ie
        .navigate "http://www.google.com"
        .Visible = True
        While .Busy Or .readyState <> READYSTATE_COMPLETE
           DoEvents
        Wend
        Set doc = .document
        DoEvents
    End With
    ***ele_exist ("byname","btnK")*** - THIS IS WHERE SYNTAX ERROR IS DISPLAYED AND THE CODE IS DISPLAYED IN RED

End Sub

I even tried converting it to a boolean FUnction rather than sub, but no luck.

Please help

How to&Answers:

As I mentione in comments, change

ele_exist ("byname","btnK")

to

ele_exist "byname","btnK"

or

Call ele_exist ("byname","btnK")

One more possible way is to use named parameters:

ele_exist val:="byname", ele:="btnK"

For additional explanation check my another post: What is the difference between entering parameters in these four different ways