在vba中调用sub时出错

时间:2014-07-04 12:28:14

标签: excel vba excel-vba

被困,无法继续这样做。请在下面找到我的代码。代码基本上是通过VBA验证网页中是否存在元素。我创建了以下子。

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

现在,当我调用此子时,它会给出syntax error

这是我称之为上述子

的地方
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

我甚至尝试将其转换为布尔值而不是sub,但没有运气。

请帮忙

1 个答案:

答案 0 :(得分:3)

正如我在评论中提到的那样,改变

ele_exist ("byname","btnK")

ele_exist "byname","btnK"

Call ele_exist ("byname","btnK")

另一种可能的方法是使用命名参数:

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

有关其他说明,请查看我的另一篇文章:What is the difference between entering parameters in these four different ways