InStr 函数InStr function

返回一个 **Variant **(Long) 值,指定一个字符串在另一个字符串中首次出现的位置。Returns a Variant (Long) specifying the position of the first occurrence of one string within another.


有兴趣开发跨多个平台扩展 Office 体验的解决方案吗?Interested in developing solutions that extend the Office experience across multiple platforms? 查看新的 Office 外接程序模型Check out the new Office Add-ins model. 与VSTO外接程序和解决方案相比,Office外接程序占用的空间较小,您可以使用几乎任何Web编程技术(例如HTML5,JavaScript,CSS3和XML)来构建它们。Office Add-ins have a small footprint compared to VSTO Add-ins and solutions, and you can build them by using almost any web programming technology, such as HTML5, JavaScript, CSS3, and XML.


InStr([ start ], string1, string2, [ compare ])InStr([ start ], string1, string2, [ compare ])

InStr 函数语法有以下参数The InStr function syntax has these arguments:

PartPart 说明Description
startstart 可选。Optional. 设置每次搜索的起始位置的数字表达式Numeric expression that sets the starting position for each search. 如果忽略,则搜索从第一个字符位置开始。If omitted, search begins at the first character position. 如果 start 包含 Null,则出现错误。If start contains Null, an error occurs. 如果指定了 compare,则 start 参数是必需的。The start argument is required if compare is specified.
string1string1 必需。Required. 要搜索的字符串表达式String expression being searched.
string2string2 必需。Required. 搜索到的字符串表达式。String expression sought.
comparecompare 可选。Optional. 指定字符串比较的类型。Specifies the type of string comparison. 如果 compareNull,则将发生错误。If compare is Null, an error occurs. 如果省略 compare,则 Option Compare 设置将决定比较的类型。If compare is omitted, the Option Compare setting determines the type of comparison. 指定有效的 LCID (LocaleID) 以在比较中使用区域设置特定规则。Specify a valid LCID (LocaleID) to use locale-specific rules in the comparison.


compare 参数设置如下。The compare argument settings are as follows.

常量Constant Value 说明Description
vbUseCompareOptionvbUseCompareOption -1-1 使用 Option Compare 语句的设置执行比较。Performs a comparison by using the setting of the Option Compare statement.
vbBinaryComparevbBinaryCompare 00 执行二进制比较。Performs a binary comparison.
vbTextComparevbTextCompare 11 执行文本比较。Performs a textual comparison.
vbDatabaseComparevbDatabaseCompare 22 仅用于 Microsoft Access。Microsoft Access only. 根据数据库中的信息执行比较。Performs a comparison based on information in your database.

返回值Return values

IfIf InStr 返回InStr returns
string1 是零长度string1 is zero-length 00
string1Nullstring1 is Null NULLNull
string2 是零长度string2 is zero-length startstart
string2Nullstring2 is Null NULLNull
未找到 string2string2 is not found 00
string1 中找到 string2string2 is found within string1 找到匹配的位置Position at which match is found
start > string2start > string2 00


InStrB 函数适用于包含在字符串中的字节数据。The InStrB function is used with byte data contained in a string. InStrB 返回某字符串在其他字符串中首次出现的字节位置,而不返回其字符位置。Instead of returning the character position of the first occurrence of one string within another, InStrB returns the byte position.


本示例使用 InStr 函数来返回某字符串在其他字符串中首次出现的位置。This example uses the InStr function to return the position of the first occurrence of one string within another.

Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP"    ' String to search in.
SearchChar = "P"    ' Search for "P".

' A textual comparison starting at position 4. Returns 6.
MyPos = Instr(4, SearchString, SearchChar, 1)    

' A binary comparison starting at position 1. Returns 9.
MyPos = Instr(1, SearchString, SearchChar, 0)

' Comparison is binary by default (last argument is omitted).
MyPos = Instr(SearchString, SearchChar)    ' Returns 9.

MyPos = Instr(1, SearchString, "W")    ' Returns 0.

另请参阅See also

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.