如何在VBA 2003中使用InStr函数?

时间:2013-07-25 10:30:40

标签: excel vba

下面的代码在InStr调用中返回错误5:

Option Explicit

Sub Test()
     Dim wb As Workbook
     Dim wbs As Workbooks
     Dim str As String
     Dim LPosition As Integer

     Set wbs = Application.Workbooks
     For Each wb In wbs
        str = wb.Name
        LPosition = InStr(0, str, "_", vbTextCompare)
        MsgBox LPosition
     Next wb
End Sub  

有人可以解释一下原因吗?

1 个答案:

答案 0 :(得分:5)

InStr函数的起始索引是从1开始=> 0是无效值。

请参阅http://msdn.microsoft.com/en-us/library/8460tsh1%28v=vs.80%29.aspx

例外部分