【问题标题】:Excel VBA - Auto FIlter and Advanced filter usage errorExcel VBA - 自动过滤器和高级过滤器使用错误
【发布时间】:2017-04-07 10:50:18
【问题描述】:

我有一个要求,我需要先使用自动过滤器过滤数据,然后使用高级过滤器单独获取唯一值。但是高级过滤器不会单独采用自动过滤值。我如何将它们一起使用?

这是我的代码,

Colmz = WorksheetFunction.Match("RSDate", Sheets("RS_Report").Rows(1), 0)

ActiveSheet.ListObjects("RS").Range.AutoFilter Field:=Colmz, Criteria1:="YES"

ActiveSheet.Range("B1:B65536").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("CSRS").Range("B14"), Unique:=True

请纠正我并分享您的建议。谢谢

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我会将唯一值粘贴在一个数组中 - 它更快且不太可能中断 -

    sub uniquearray()
    Colmz = WorksheetFunction.Match("RSDate", Sheets("RS_Report").Rows(1), 0)
    
    ActiveSheet.ListObjects("RS").Range.AutoFilter Field:=Colmz, Criteria1:="YES"
    Call creatary(curary, Sheets("RS_Report"), Letter(Sheets("RS_Report"), "RSDate")):  Call eliminateDuplicate(curary): Call BuildArrayWithoutBlankstwo(curary): Call Alphabetically_SortArray(curary)
    
    For Each cell In curary
        'do what you need to do with the unique array list
    Next cell
    end sub
    
    Function creatary(ary As Variant, sh As Worksheet, ltr As String)
    Dim x, y, rng As Range
    ReDim ary(0)
    
    Set rng = sh.Range(ltr & "2:" & ltr & sh.Range("A1000000").End(xlUp).Row).SpecialCells(xlCellTypeVisible)
    
    x = 0
    For Each y In rng
        If Not Application.IsError(y) Then
                If Not IsNumeric(y) Then
                    ary(x) = y
                End If
                x = x + 1
            ReDim Preserve ary(x)
        End If
    Next y
    End Function
    
    Function BuildArrayWithoutBlankstwo(ary As Variant)
    Dim AryFromRange() As Variant, AryNoBlanks() As Variant
    Dim Counter As Long, NoBlankSize As Long
    
    'set references and initialize up-front
    ReDim AryNoBlanks(0 To 0)
    NoBlankSize = 0
    
    'load the range into array
    AryFromRange = ary
    
    'loop through the array from the range, adding
    'to the no-blank array as we go
    For Counter = LBound(AryFromRange) To UBound(AryFromRange)
        If ary(Counter) <> 0 Then
            NoBlankSize = NoBlankSize + 1
            AryNoBlanks(UBound(AryNoBlanks)) = ary(Counter)
            ReDim Preserve AryNoBlanks(0 To UBound(AryNoBlanks) + 1)
        End If
    Next Counter
    
    'remove that pesky empty array field at the end
    If UBound(AryNoBlanks) > 0 Then
        ReDim Preserve AryNoBlanks(0 To UBound(AryNoBlanks) - 1)
    End If
    
    'debug for reference
    ary = AryNoBlanks
    
    End Function
    
    Function eliminateDuplicate(ary As Variant) As Variant
    Dim aryNoDup(), dupArrIndex, i, dupBool, j
    
    
        dupArrIndex = -1
    For i = LBound(ary) To UBound(ary)
            dupBool = False
    
            For j = LBound(ary) To i
                If ary(i) = ary(j) And Not i = j Then
                    dupBool = True
                End If
            Next j
    
            If dupBool = False Then
                dupArrIndex = dupArrIndex + 1
                ReDim Preserve aryNoDup(dupArrIndex)
                aryNoDup(dupArrIndex) = ary(i)
            End If
    Next i
    
    ary = aryNoDup
    End Function
    
    Function Alphabetically_SortArray(ary)
    
    Dim myArray As Variant
    Dim x As Long, y As Long
    Dim TempTxt1 As String
    Dim TempTxt2 As String
    
    myArray = ary
    
    'Alphabetize Sheet Names in Array List
      For x = LBound(myArray) To UBound(myArray)
        For y = x To UBound(myArray)
          If UCase(myArray(y)) < UCase(myArray(x)) Then
            TempTxt1 = myArray(x)
            TempTxt2 = myArray(y)
            myArray(x) = TempTxt2
            myArray(y) = TempTxt1
          End If
         Next y
      Next x
    
    ary = myArray
    End Function
    
    Function Letter(oSheet As Worksheet, name As String, Optional num As Integer)
    If num = 0 Then num = 1
    Letter = Application.Match(name, oSheet.Rows(num), 0)
    Letter = Split(Cells(, Letter).Address, "$")(1)
    End Function
    

    【讨论】:

    • 请注意,您需要将正确的参数传递给函数。此外,字母排序可能不适用于数字列
    • 这很酷。但我不是写作功能的专家。你能根据我的要求改变它吗?或者你能纠正我的代码。谢谢
    • 现在试试?我试图用你的代码更新它,你最终应该得到一个名为 curary 的数组,其中包含你的唯一值。在那之后你用它做什么是另一回事
    • 非常感谢先生!
    • 没问题,也只是试着记住,函数让生活变得如此简单,因为你不必重写代码!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多