[英]Run-time Error '1004'-- AutoFilter Method of Range Class Failed
I am pulling a work organization report and want to find and filter by a unique ID value.我正在拉一份工作组织报告,并希望按唯一 ID 值查找和过滤。 The unique ID to be filtered is specified by a public property (this number is used on another occasion so that is why it is public) entered through a text box within a userform.
要过滤的唯一 ID 由通过用户表单中的文本框输入的公共属性指定(此编号用于其他场合,因此它是公共的)。 The file being used is a dynamically named report that the user selects from an open file dialog.
正在使用的文件是用户从打开的文件对话框中选择的动态命名的报告。
There are 9 different manager levels I am filtering through, columns AU, AW, AY, BA ,BC, BE, BG, BI, & BK, and they all rest on row 3. Therefore I have columns 'A3:BK3' but am only filtering between 'AU3:BK3' to pull data in the earlier columns.我过滤了 9 个不同的经理级别,AU、AW、AY、BA、BC、BE、BG、BI 和 BK 列,它们都位于第 3 行。因此我有列“A3:BK3”但我仅在 'AU3:BK3' 之间进行过滤以提取较早列中的数据。
Problem: I am continuing to receive a "Run-time error '1004': AutoFilter method of Range class failed" and I have no idea why, even after trying to debug.问题:我继续收到“运行时错误‘1004’:Range 类的自动筛选方法失败”,我不知道为什么,即使在尝试调试之后也是如此。 It works on some columns, but not on others when using test IDs that are throughout the 9 columns.
当使用整个 9 列的测试 ID 时,它适用于某些列,但不适用于其他列。
Code:代码:
Private Sub EmailButton_Click()
'test WWIDS
'75305 -- 337431 -- 152820578 -- 152821156
Application.ScreenUpdating = False
'Filter by WWID -- Question: How to know who to pull for?
'Filter criteria (Unique ID - WWID), if it does not exist, then move to next, else end/stop
If Len(Trim(Me.EnterWWIDtxtbox.Text)) = 0 Then
Me.EnterWWIDtxtbox.SetFocus
MsgBox "Must provide a Unique ID"
Exit Sub
End If
Dim ws As Worksheet
Dim wb As Workbook
Set wb = Workbooks.Open(Filename:=OpenFileTxt)
Set ws = wb.Worksheets("Sheet1")
With wb.Worksheets("Sheet1")
Dim aColumns() As String
aColumns = Split("AU,AW,AY,BA,BC,BE,BG,BK,BI", ",")
Dim bFound As Boolean
bFound = False
Dim rFound As Range
Dim vColumn As Variant
For Each vColumn In aColumns
Set rFound = ws.Columns(vColumn).Find(WWID, , xlValues, xlPart)
If Not rFound Is Nothing Then
bFound = True
MsgBox "Found [" & WWID & "] in column " & vColumn
With ws.Columns(vColumn)
.AutoFilter 1, rFound.Value
End With
Exit For
End If
Next vColumn
If bFound = False Then MsgBox "Unique ID [" & WWID & "] not found"
End With
I'm new to VBA just FYI, I tackle problems to learn, but my answer may not be perfect.我是 VBA 新手,仅供参考,我解决了要学习的问题,但我的答案可能并不完美。
TLDR:域名注册地址:
Use: With ws.Range(rFound.Address(False, False))
NOT With ws.Columns(vColumn)
使用:
With ws.Range(rFound.Address(False, False))
不With ws.Columns(vColumn)
You have a bunch of repeating code which I tried to trim down, but as I'm not 100% on the end goal or how things work, I could only do so much.你有一堆重复的代码,我试图削减它们,但由于我不是 100% 的最终目标或事情如何运作,我只能做这么多。 Here is what I ended up with.
这就是我的结果。
Private Sub EmailButton_Click()
'Get WWID
WWID = "111"
'WWID = "777" ' HardCode for Testing
Dim ws As Worksheet
Dim wb As Workbook
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet
Dim aColumns() As String
aColumns = Split("AU,AW,AY,BA,BC,BE,BG,BI,BK", ",")
If WWID = "111" Then
Col = "47" 'AU = "47"
End If
AW = "49"
AY = "51"
BA = "53"
BC = "55"
BE = "57"
If WWID = "777" Then
Col = "59" 'BG = "59"
End If
BI = "61"
BK = "63"
ws.AutoFilterMode = False
Dim rFound As Range
Dim vColumn As Variant
For Each vColumn In aColumns
Set rFound = ws.Columns(vColumn).Find(WWID, , xlValues, xlPart)
If Not rFound Is Nothing Then
With ws.Range(rFound.Address(False, False))
.AutoFilter Col, rFound.Value
End With
End If
Next vColumn
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.