[英]Excel: Using Advanced Distinct Filter on already filtered Range [1004: Database or table range is not valid.]
I try to filter my list to all entries with "1" and the items that match that criteria I want to be listed uniquely to iterate through that list later.我尝试将我的列表过滤到所有带有“1”的条目以及与我希望唯一列出的条件匹配的项目,以便稍后遍历该列表。
But I get the error message: 1004 "Database or table range is not valid."但我收到错误消息:1004“数据库或表范围无效。”
This is the code I am trying to use:这是我尝试使用的代码:
Sub Schritt_42temp()
With Sheets("Tabelle1")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
ActiveSheet.Range("$A$1:$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
It works if I don't filter the list, but it's important that he only selects the filtered items:如果我不过滤列表,它会起作用,但重要的是他只选择过滤后的项目:
Sub Schritt_42temp()
With Sheets("Tabelle1")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
'Disable Filtering
'ActiveSheet.Range("$A$1:$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
Please note that I am a total beginner to VBA programming.请注意,我是 VBA 编程的初学者。 Do you have any advice for me?
你对我有什么建议吗?
since your goal is to have a list to unique values to iterate through, you can use Dictionary
object to get it因为您的目标是有一个唯一值列表进行迭代,您可以使用
Dictionary
object 来获取它
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.