WorksheetBase.AutoFilter 屬性

取得 Microsoft.Office.Interop.Excel.AutoFilter,在啟用了篩選功能的情況下,提供工作表上篩選後清單的相關資訊。 如果關閉篩選,則會取得 nullnull 參考 (即 Visual Basic 中的 Nothing)。

命名空間:  Microsoft.Office.Tools.Excel
組件:  Microsoft.Office.Tools.Excel.v4.0.Utilities (在 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 中)

語法

'宣告
Public ReadOnly Property AutoFilter As AutoFilter
public AutoFilter AutoFilter { get; }

屬性值

型別:Microsoft.Office.Interop.Excel.AutoFilter
Microsoft.Office.Interop.Excel.AutoFilter 可以在啟用了篩選功能的情況下,提供工作表上篩選後清單的相關資訊,否則為 nullnull 參考 (即 Visual Basic 中的 Nothing)。

備註

若要以程式設計方式啟用篩選,請使用 Range 物件的 AutoFilter 方法或 NamedRange 物件的 AutoFilter 方法。

範例

在下列程式碼範例中,建立了以 Range 範圍篩選的水果清單,然後使用 AutoFilter 屬性顯示清單的篩選準則。 這個範例接著會提示使用者利用 AutoFilterMode 屬性關閉篩選,最後再使用 FilterMode 屬性顯示工作表是否有篩選的清單。

這是示範文件層級自訂的範例。

Private Sub ActivateAutoFilter()
    Me.Range("A1").Value2 = "Fruits"
    Me.Range("A2").Value2 = "Banana"
    Me.Range("A3").Value2 = "Apple"
    Me.Range("A4").Value2 = "Banana"
    Me.Range("A5").Value2 = "Orange"
    Me.Range("A6").Value2 = "Apple"
    Me.Range("A7").Value2 = "Orange"

    Dim NamedRange1 As Microsoft.Office.Tools.Excel.NamedRange = _
        Me.Controls.AddNamedRange(Me.Range("A1", "A7"), "NamedRange1")

    NamedRange1.AutoFilter(1, "Apple", Excel.XlAutoFilterOperator.xlAnd, _
        VisibleDropDown:=True)

    MsgBox("The current criteria for the AutoFilter is: " & _
        Me.AutoFilter.Filters(1).Criteria1.ToString())

    If Me.AutoFilterMode Then
        If DialogResult.Yes = MessageBox.Show("Would you like to " & _
            "turn off the filter?", "Example", MessageBoxButtons.YesNo) Then
            Me.AutoFilterMode = False
        End If
    End If

    If Me.FilterMode Then
        MsgBox("The worksheet has a filtered list.")
    Else
        MsgBox("The worksheet does not have a filtered list")
    End If
End Sub
private void ActivateAutoFilter()
{
    this.Range["A1"].Value2 = "Fruits";
    this.Range["A2"].Value2 = "Banana";
    this.Range["A3"].Value2 = "Apple";
    this.Range["A4"].Value2 = "Banana";
    this.Range["A5"].Value2 = "Orange";
    this.Range["A6"].Value2 = "Apple";
    this.Range["A7"].Value2 = "Orange";

    Microsoft.Office.Tools.Excel.NamedRange NamedRange1 =
        this.Controls.AddNamedRange(this.Range["A1", "A7"],
        "NamedRange1");

    NamedRange1.AutoFilter(1, "Apple",
       Excel.XlAutoFilterOperator.xlAnd, true);

    MessageBox.Show("The current criteria for the AutoFilter is: " +
        this.AutoFilter.Filters[1].Criteria1.ToString());

    if (this.AutoFilterMode)
    {
        if (DialogResult.Yes == MessageBox.Show("Would you like to " +
            "turn off the filter?", "Example", MessageBoxButtons.YesNo))
        {
            this.AutoFilterMode = false;
        }
    }

    if (this.FilterMode)
    {
        MessageBox.Show("The worksheet has a filtered list.");
    }
    else
    {
        MessageBox.Show("The worksheet does not have a filtered list");
    }
}

.NET Framework 安全性

請參閱

參考

WorksheetBase 類別

Microsoft.Office.Tools.Excel 命名空間

AutoFilterMode

FilterMode

AutoFilter

ShowAllData