XmlMappedRange.Find 方法

定義

尋找 XmlMappedRange 控制項中的指定資訊,並且在找到第一個儲存格的資訊時傳回 Range 物件。

public Microsoft.Office.Interop.Excel.Range Find (object What, object After, object LookIn, object LookAt, object SearchOrder, Microsoft.Office.Interop.Excel.XlSearchDirection SearchDirection = Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, object MatchCase, object MatchByte, object SearchFormat);
abstract member Find : obj * obj * obj * obj * obj * Microsoft.Office.Interop.Excel.XlSearchDirection * obj * obj * obj -> Microsoft.Office.Interop.Excel.Range
Public Function Find (What As Object, Optional After As Object, Optional LookIn As Object, Optional LookAt As Object, Optional SearchOrder As Object, Optional SearchDirection As XlSearchDirection = Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, Optional MatchCase As Object, Optional MatchByte As Object, Optional SearchFormat As Object) As Range

參數

What
Object

要搜尋的資料。 可以是字串或任何 Microsoft Office Excel 資料類型。

After
Object

搜尋起點後的儲存格。 這個儲存格就是從使用者介面進行搜尋時的現用儲存格位置。 請注意, After 必須是範圍中的單一單元格。 請記住,搜尋會在這個儲存格後開始,而直到此方法繞回指定儲存格後,才會搜尋這個儲存格。 如果您未指定這個引數,則會從範圍左上角的儲存格後開始搜尋。

LookIn
Object

資訊類型。

LookAt
Object

可以是下列其中一個 XlLookAt 值:xlWholexlPart

SearchOrder
Object

可以是下列其中一個 XlSearchOrder 值:xlByRowsxlByColumns

SearchDirection
XlSearchDirection

搜尋方向。可以是下列 XlSearchDirection 其中一個值: xlNextxlPrevious

MatchCase
Object

true 表示搜尋時區分大小寫。 預設值是 false

MatchByte
Object

只有在您選取或安裝雙位元語言支援時才能使用。 true 表示只會將雙位元組字元與雙位元組字元比對,否則為 false,表示將雙位元組字元與其對等的單位元組字元比對。

SearchFormat
Object

搜尋格式。

傳回

Range 表示找到含有指定資訊的第一個儲存格。

範例

下列程式代碼範例會將 的值 XmlMappedRange 設定為字串 「Smith」,然後使用 FindFindNextFindPrevious 方法來尋找具有字串 「Smith」 的第一個單元格。 因為 一 XmlMappedRange 律只包含一個單元格,所以在每個案例中都會找到相同的單元格。 這個程式代碼範例假設目前的工作表包含 XmlMappedRange 名為 CustomerLastNameCell的 。

private void FindSmith()
{
    this.CustomerLastNameCell.Value2 = "Smith";

    // Use Find to get the range with "Smith".
    Excel.Range range1 = this.CustomerLastNameCell.Find("Smith",
        Excel.XlSearchDirection.xlNext);
    string address1 = range1.get_Address(missing, missing,
        Excel.XlReferenceStyle.xlA1);
    MessageBox.Show("Find method found the range: " + address1);

    // Use FindNext to get the range with "Smith".
    Excel.Range range2 = this.CustomerLastNameCell.FindNext(range1);
    string address2 = range2.get_Address(
        Excel.XlReferenceStyle.xlA1);
    MessageBox.Show("FindNext method found the range: " + address2);

    // Use FindPrevious to get the range with "Smith".
    Excel.Range range3 = this.CustomerLastNameCell.FindPrevious(range2);
    string address3 = range3.get_Address(
        Excel.XlReferenceStyle.xlA1);
    MessageBox.Show("FindPrevious method found the range: " + address3);
}
Private Sub FindSmith()
    Me.CustomerLastNameCell.Value2 = "Smith"

    ' Use Find to get the range with "Smith".
    Dim range1 As Excel.Range = Me.CustomerLastNameCell.Find( _
        "Smith", SearchDirection:=Excel.XlSearchDirection.xlNext)
    Dim address1 As String = range1.Address(ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("Find method found the range: " & address1)

    ' Use FindNext to get the range with "Smith".
    Dim range2 As Excel.Range = Me.CustomerLastNameCell.FindNext(range1)
    Dim address2 As String = range2.Address(ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("FindNext method found the range: " & address2)

    ' Use FindPrevious to get the range with "Smith".
    Dim range3 As Excel.Range = Me.CustomerLastNameCell.FindPrevious(range2)
    Dim address3 As String = range3.Address(ReferenceStyle:=Excel.XlReferenceStyle.xlA1)
    MsgBox("FindPrevious method found the range: " & address3)
End Sub

備註

如果找不到相符項目,這個方法會 null 傳回 。

此方法不會影響選取範圍或使用中格。

LookIn每次使用此方法時,都會儲存 、LookAtSearchOrderMatchByte 的設定。 如果您未指定這些自變數的值,下次呼叫 方法時會使用儲存的值。 設定這些自變數會變更 [尋找] 對話框中的設定,並在 [尋找] 對話框中變更儲存的值,而如果您省略自變數,則會變更所使用的儲存值。 若要避免問題,請在每次使用此方法時明確設定這些自變數。

您可以使用 Microsoft.Office.Interop.Excel.Range.FindNext*FindPrevious 方法來重複搜尋。

適用於