我想通过使用行业代码(例如C10.3、H53.1等)来筛选一个行业列表。最后,我要我的单子只展示那些行业。
我想要过滤的数据在Tabelle1列21中。此外,我已经在Tabelle3列B上说明了我的行业代码。但是,下面的代码没有执行。
有谁知道为什么和如何使它适应我的需要吗?
Sub Autofilter()
Dim Bereich As Range
Dim IndustryCode As Variant
Set Bereich = Tabelle1.UsedRange
IndustryCode = Tabelle3.Range("B:B").Value2
Bereich.Autofilter Field:=21, Criteria1:="=" & IndustryCode
End Sub发布于 2018-03-07 14:39:46
您需要转接IndustryCode,因为
IndustryCode = Tabelle3.Range("B:B").Value2结果为二维array(1 to 1048576, 1 to 1),但标准等待一维数组。
所以之后
IndustryCode = Application.WorksheetFunction.Transpose(IndustryCode)您得到了一个一维array(1 to 65536),可以与Operator:=xlFilterValues一起作为Criteria1使用。
Sub aaa()
Dim Bereich As Range
Dim IndustryCode As Variant
Set Bereich = Tabelle1.UsedRange
IndustryCode = Tabelle3.Range("B:B").Value2
IndustryCode = Application.WorksheetFunction.Transpose(IndustryCode)
Bereich.AutoFilter Field:=21, Criteria1:=IndustryCode, Operator:=xlFilterValues
End Sub注意,使用整个列并不是很优雅,因为数组中有65536条目,这意味着大多数条目是空的。更好的方法是仅将范围用作填充数据的数组:
IndustryCode = Tabelle3.Range("B1", Tabelle3.Cells(Cells.Rows.Count, "B").End(xlUp)).Value2这将将数组简化为B列中使用的部分。
还请注意,如果IndustryCode (B列)包含真实数字,则需要将它们转换为具有数组上循环的字符串。
ary(i) = CStr(ary(i))如this answer所示。
https://stackoverflow.com/questions/49154096
复制相似问题