Excel:对已过滤的范围使用高级不同过滤器 [1004:数据库或 table 范围无效。]

Excel: Using Advanced Distinct Filter on already filtered Range [1004: Database or table range is not valid.]

我尝试将我的列表过滤为包含“1”的所有条目以及符合该条件的项目我希望唯一列出以便稍后遍历该列表。

但我收到错误消息:1004 "Database or table range is not valid."

这是我尝试使用的代码:

Sub Schritt_42temp()

With Sheets("Tabelle1")
    lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With

    ActiveSheet.Range("$A:$BZ$" & lastRow).AutoFilter Field:=60, Criteria1:=1

    Range("A2:A" & lastRow).SpecialCells(xlCellTypeVisible).Select
    Selection.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("BG2:BG6000"), Unique:=True

End Sub 

如果我不过滤列表,它会起作用,但重要的是他只选择过滤的项目:

Sub Schritt_42temp()

With Sheets("Tabelle1")
    lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With

   'Disable Filtering
   'ActiveSheet.Range("$A:$BZ$" & lastRow).AutoFilter Field:=60, Criteria1:=1

    Range("A2:A" & lastRow).SpecialCells(xlCellTypeVisible).Select
    Selection.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("BG2:BG6000"), Unique:=True

End Sub 

请注意,我是 VBA 编程的初学者。 你对我有什么建议吗?

由于您的目标是要有一个唯一值列表来迭代,您可以使用 Dictionary 对象来获取它

Sub Schritt_42temp()
    Dim cel As Range
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")

    With Sheets("Tabelle1")

        With .Range("BZ1", Cells(.Rows.Count, 1).End(xlUp))
            .AutoFilter Field:=60, Criteria1:=1
            For Each cel In .Columns(1).Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
                dict(cel.Value) = dict(cel.Value) & vbNullString
            Next
        End With
        .AutoFilterMode = False

        Dim valuesToIterate As Variant
        valuesToIterate = dict.keys '<-- here you get an array filled with column A unique values corresponding to column BH 1's

    End With
End Sub