XmlMappedRange.Find 方法

XmlMappedRange 控件中查找指定信息,并返回表示在其中找到该信息的第一个单元格的 Range

命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel(在 Microsoft.Office.Tools.Excel.dll 中)

语法

声明
Function Find ( _
    What As Object, _
    After As Object, _
    LookIn As Object, _
    LookAt As Object, _
    SearchOrder As Object, _
    SearchDirection As XlSearchDirection, _
    MatchCase As Object, _
    MatchByte As Object, _
    SearchFormat As Object _
) As Range
Range Find(
    Object What,
    Object After,
    Object LookIn,
    Object LookAt,
    Object SearchOrder,
    XlSearchDirection SearchDirection,
    Object MatchCase,
    Object MatchByte,
    Object SearchFormat
)

参数

  • What
    类型:System.Object
    要搜索的数据。可以是字符串或任何 Microsoft Office Excel 数据类型。
  • After
    类型:System.Object
    从其后开始搜索的单元格。此单元格就是从用户界面执行的搜索完成后处于活动状态的单元格。注意,After 必须是该范围中的单个单元格。请记住,搜索将从该单元格之后开始;只有方法绕回到指定的单元格之后,才会搜索该单元格。如果您未指定此参数,则从该范围左上角中的单元格之后开始搜索。
  • MatchCase
    类型:System.Object
    如果在搜索时区分大小写,则为 true。默认值为 false。
  • MatchByte
    类型:System.Object
    仅当已选择或安装双字节语言支持时,才会用到此参数。如果让双字节字符仅与双字节字符相匹配,则为 true;如果让双字节字符与其单字节等效字符相匹配,则为 false。

返回值

类型:Microsoft.Office.Interop.Excel.Range
一个 Range,表示在其中找到指定信息的第一个单元格。

备注

如果未找到匹配,则此方法返回 nullnull 引用(在 Visual Basic 中为 Nothing)。

此方法不会影响选定内容或活动单元格。

每当使用此方法时,都会保存 LookIn、LookAt、SearchOrder 和 MatchByte 的设置。 如果未指定这些参数的值,则在下次调用该方法时会使用保存的值。 设置这些参数会更改“查找”对话框中的设置,而更改“查找”对话框中的这些设置会更改在省略这些参数时所使用的保存值。 为了避免出现问题,请在每次使用此方法时显式设置这些参数。

可以使用 FindNextFindPrevious 方法重复搜索。

示例

下面的代码示例将 XmlMappedRange 的值设置为字符串“Smith”,然后使用 Find、FindNextFindPrevious 方法查找第一个带有字符串“Smith”的单元格。 因为 XmlMappedRange 总是正好包含一个单元格,所以在每种情况下都会找到同一个单元格。 此代码示例假定当前工作表包含一个名为 CustomerLastNameCell 的 XmlMappedRange

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
private void FindSmith()
{
    this.CustomerLastNameCell.Value2 = "Smith";

    // Use Find to get the range with "Smith".
    Excel.Range range1 = this.CustomerLastNameCell.Find("Smith",
        missing, missing, missing, missing, Excel.XlSearchDirection.xlNext,
        missing, missing, missing);
    string address1 = range1.get_Address(missing, missing,
        Excel.XlReferenceStyle.xlA1, missing, missing);
    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(missing, missing, 
        Excel.XlReferenceStyle.xlA1, missing, missing);
    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(missing, missing,
        Excel.XlReferenceStyle.xlA1, missing, missing);
    MessageBox.Show("FindPrevious method found the range: " + address3);
}

.NET Framework 安全性

请参见

参考

XmlMappedRange 接口

Microsoft.Office.Tools.Excel 命名空间