I am pulling a work organization report and want to find and filter by a unique ID value. 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. 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.
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. It works on some columns, but not on others when using test IDs that are throughout the 9 columns.
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.
TLDR:
Use: With ws.Range(rFound.Address(False, False))
NOT 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. 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