亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Excel VBA自動過濾除三個以外的所有內容

Excel VBA自動過濾除三個以外的所有內容

大話西游666 2020-02-03 12:40:54
在我的數據分析的持續傳奇中(第一個問題),我想刪除部門(字段7)不是101、102或103(名稱已更改以保護無辜)的所有行。數據中大約有一百個部門,因此使用Criteria1:=Array("104", "105", "106",etc是不切實際的。我想做這樣的事情:myrange.AutoFilter Field:=7, Criteria1:="<>101", Operator:=xlOr, _    Criteria2:="<>102", Operator:=xlOr, Criteria3:="<>103"但Excel識別的標準不超過2個。我可以添加一個幫助器列,并使宏遍歷每行(如果是101、102或103,則value = Yes),過濾出yes,然后刪除所有剩余的內容,但我將其保存為最后采取。有沒有辦法使自動篩選條件1不等于數組?就像是:myrange.AutoFilter Field:=7, Criteria1:="<>" & Array("101", "102", "103")
查看完整描述

3 回答

?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

記住目標是刪除不匹配的行;自動篩選只是幫助實現該目標的一種工具。如果自動篩選不能滿足您的需求,請選擇其他方法。考慮:


Sub AllBut()

    Dim rTable As Range, r As Range

    Dim rDelete As Range

    Set rTable = Selection

    Set rDelete = Nothing

    For Each r In rTable.Columns(7).Cells

        v = r.Value

        If v <> "101" And v <> "102" And v <> "103" Then

            If rDelete Is Nothing Then

                Set rDelete = r

            Else

                Set rDelete = Union(r, rDelete)

            End If

        End If

    Next


    If Not rDelete Is Nothing Then rDelete.EntireRow.Delete

End Sub

在這里,我們選擇要處理的數據塊(不包括標題行)。宏向下掃描該塊的第7列,并刪除任何不符合條件的行。


剩下的將是101、102和103。


查看完整回答
反對 回復 2020-02-03
?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

由于這是關于AutoFilter方法的,因此我將提供這種方法,其中涉及使用Scripting.Dictionary對象來模擬在工作表上手動執行該過程。


在工作表上,用戶將應用“自動篩選”,然后使用G列的下拉菜單“關閉” 101、102和103值。剩下的將被刪除。在VBA中,我們可以獲取所有G列,并使用非101、102或103的值填充字典對象,并將其用作過濾操作的標準。


Sub filterNotThree()

    Dim d As Long, dDELs As Object, vVALs As Variant


    Set dDELs = CreateObject("Scripting.Dictionary")


    With Worksheets("Sheet6")

        If .AutoFilterMode Then .AutoFilterMode = False

        With .Cells(1, 1).CurrentRegion

            'grab all of column G (minus the header) into a variant array

            vVALs = .Resize(.Rows.Count - 1, 1).Offset(1, 6).Value2


            'populate the dictionary object with the values that are NOT 101, 102, or 103

            For d = LBound(vVALs, 1) To UBound(vVALs, 1)

                Select Case vVALs(d, 1)

                    Case 101, 102, 103

                        'do not add

                    Case Else

                        'not a match, add it to the delete list

                        'the AutoFilter criteria needs to be text

                        ' so we set the Keys as text and the Items as numbers

                        dDELs.Item(CStr(vVALs(d, 1))) = vVALs(d, 1)

                End Select

            Next d


            'check to make sure there is something to filter on

            If CBool(dDELs.Count) Then

                'filter on the dictionary keys

                .AutoFilter field:=7, Criteria1:=dDELs.keys, Operator:=xlFilterValues


                'delete the visible rows (there has to be some)

                .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0).EntireRow.Delete

            End If


        End With

        If .AutoFilterMode Then .AutoFilterMode = False

    End With


    dDELs.RemoveAll: Set dDELs = Nothing

End Sub


查看完整回答
反對 回復 2020-02-03
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

我在做類似的事情,但在兩個領域,這種語法對我有用:


myrange.AutoFilter Field:=7, Criteria1:="<>101", Operator:=xlAnd, Criteria2:="<>102", Operator:=xlAnd

希望能幫助到你。


查看完整回答
反對 回復 2020-02-03
  • 3 回答
  • 0 關注
  • 938 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號