简体   繁体   English

使用标题名称的自动筛选字段

[英]Autofilter Field using the header Name

i tried to use the Autofilter function but using field with the header name "ID" instead the number of column. 我尝试使用自动过滤器功能,但使用标题名称为“ ID”的字段而不是列数。 i tried my best and i got this error message "Advanced filter fails at iterations, run-time error 1004" 我已尽力而为,并收到此错误消息“高级筛选器在迭代中失败,运行时错误1004”

I am stuck for two days with it . 我被困了两天。 thnk's for your help 谢谢你的帮助

code : 代码:

Sub AdataPreparation()
Dim WorkBk As Workbook, WorkSh As Worksheet, WrkTab As range, FilterRow As Variant
Set WorkBk = Workbooks.Open(Filename:="C:\Users\Documents\DataApplied.xlsm")
Set WorkSh = Sheets("sheet2") 
WorkSh.Activate 
Set WrkTab = range("A1").CurrentRegion 
WrkTab = ActiveRange
FilterRow = Application.Match("ID", WrkTab, 0)
Selection.AutoFilter Field:=FilterRow, Criteria1:="="
End Sub

The issue probably lies in not having anything selected for your .Autofilter to filter. 问题可能在于没有为您的.Autofilter选择要过滤的任何内容。 Try replacing Selection with a range, or the .UsedRange . 尝试用范围或.UsedRange替换Selection

You also don't need WrkTab , I don't see it having any purpose - here I use .Find instead: 您也不需要WrkTab ,我看不到它有任何用途-在这里我使用.Find代替:

Sub AdataPreparation()
Dim WorkBk As Workbook, WorkSh As Worksheet, FilterRow As Variant

Set WorkBk = Workbooks.Open(Filename:="C:\Users\Documents\DataApplied.xlsm")
Set WorkSh = Sheets("sheet2")

WorkSh.Activate

FilterRow = Rows("1:1").Find(What:="ID", LookAt:=xlWhole).Column

WorkSh.UsedRange.AutoFilter Field:=FilterRow, Criteria1:="="
End Sub

I should add that it would be best for you to explicitly refer to your range instead of using UsedRange 我应该补充一点,最好是显式引用您的范围,而不是使用UsedRange

This may help you: 这可以帮助您:

Sub Macro1()
    Set r = Range("A1").CurrentRegion
    r.AutoFilter
    kolumn = r.Find(what:="ID", after:=r(1)).Column
    r.AutoFilter Field:=kolumn, Criteria1:="="
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM