下面的代码在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
有人可以解释一下原因吗?
答案 0 :(得分:5)
InStr
函数的起始索引是从1开始=> 0是无效值。
请参阅http://msdn.microsoft.com/en-us/library/8460tsh1%28v=vs.80%29.aspx
的例外部分