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. Office 增益集與 VSTO 增益集和解決方案相比具有較小的足跡,您也可以使用幾乎任何 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:

部分Part 描述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. 如果省略 compareOption 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
string2 找不到string2 is not found 00
string2string1 中找到string2 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.