AutoFilter Field Macro Returns a Header Row only

NorthbyNorthwest

Board Regular
Joined
Oct 27, 2013
Messages
173
Office Version
  1. 365
Hi, everyone. I have a worksheet and code below. See pic attached. I cannot get auto filter to work. Every time I run macro it returns the header row only. Could someone help me? I've tried checking various columns and the code itself, and I come up with nothing.

VBA Code:
Sub FilterByMonth()
Dim MyRange As Range
Dim CalcMode As Long
Dim ViewMode As Long
Dim lrow As Long
Dim FilterCriteria As String
Dim FilterCriteria2 As String
Dim NewBook As Workbook

'Filter MTD Accuracy
Sheet1.Activate
Sheet1.Unprotect "hideaway"
lrow = Cells.Find(What:="*", After:=Range("B7"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, MatchCase:=False).Row

Set MyRange = Range(Cells(6, 2), Cells(lrow, 13))
MyRange.Parent.Select

If ActiveWorkbook.ProtectStructure = True Or MyRange.Parent.ProtectContents = True Then
    MsgBox "Sorry, not working when the workbook or worksheet is protected", vbOKOnly, "Remove Protection"
    Exit Sub
End If

'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False

'Firstly, remove the AutoFilter
MyRange.Parent.AutoFilterMode = False

'Filter on a Inputbox value 1
FilterCriteria = InputBox("What month do you want to filter on?", "Enter the fiscal month number 1 thru 12.")

'Filter on a Inputbox value 2
FilterCriteria2 = InputBox("What audit group do you want to filter on?", "Enter either *Alpha* or *Beta*.")

With MyRange
    .AutoFilter Field:=12, Criteria1:="=" & FilterCriteria
    .AutoFilter Field:=5, Criteria1:="=" & FilterCriteria2
End With

Set NewBook = Workbooks.Add
ThisWorkbook.Sheets("MTD Accuracy").AutoFilter.Range.Copy
    With NewBook.Worksheets("Sheet1")
        .Range("B3").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
        .Range("B3").PasteSpecial Paste:=xlPasteFormats
        .Columns("A").ColumnWidth = 5
        .Columns("B").ColumnWidth = 10
        .Columns("C:D").ColumnWidth = 35
        .Columns("E").ColumnWidth = 10
        .Columns("F").ColumnWidth = 19
        .Columns("G").ColumnWidth = 10
        .Columns("H").ColumnWidth = 22
        .Columns("I:L").ColumnWidth = 13
        .Columns("M").ColumnWidth = 10
        .Columns("M").Hidden = True
        .Rows("3:3").RowHeight = 46
        .Rows("4:204").RowHeight = 20
        .Range("A1").Select
    End With
Application.CutCopyMode = False
ActiveWindow.DisplayGridlines = False
Sheet1.Protect "hideaway"
End Sub

Thanks,
 

Attachments

  • MonthToDate Table.jpg
    MonthToDate Table.jpg
    136.2 KB · Views: 8

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
What happens with
Rich (BB code):
.AutoFilter Field:=4, Criteria1:="=" & FilterCriteria2
 
Upvote 0
I think we will need your data posted with XL2BB or your file hosted somewhere rather than an image because
with the code below
VBA Code:
Sub FilterByMonth()
Dim MyRange As Range
Dim lrow As Long
Dim FilterCriteria As String
Dim FilterCriteria2 As String


lrow = Cells.Find(What:="*", After:=Range("B7"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, MatchCase:=False).Row

Set MyRange = Range(Cells(6, 2), Cells(lrow, 13))
MyRange.Parent.Select

'Firstly, remove the AutoFilter
MyRange.Parent.AutoFilterMode = False

'Filter on a Inputbox value 1
FilterCriteria = InputBox("What month do you want to filter on?", "Enter the fiscal month number 1 thru 12.")

'Filter on a Inputbox value 2
FilterCriteria2 = InputBox("What audit group do you want to filter on?", "Enter either *Alpha* or *Beta*.")

With MyRange
    .AutoFilter Field:=12, Criteria1:="=" & FilterCriteria
    .AutoFilter Field:=4, Criteria1:="=" & FilterCriteria2
End With

End Sub

and the data below

Book1
BCDEFGHIJKLM
6EmployeePositionGradeAudit GroupUnitSupervisorAudit MonthTotalCheckedCorrectRateFiscal Month
7Alpha8
8Alpha7
9Beta8
10Alpha8
11Beta7
12Alpha7
13Alpha8
MySheet


With FilterCriteria as 8 and FilterCriteria2 as Alpha I get

Book1
BCDEFGHIJKLM
6EmployeePositionGradeAudit GroupUnitSupervisorAudit MonthTotalCheckedCorrectRateFiscal Month
7Alpha8
10Alpha8
13Alpha8
MySheet
 
Upvote 0
VBA Code:
LookIn:=xlFormulas

Are your cells values or are any columns formulas?
 
Upvote 0
Then I'll definitely need the worksheet posted either with XL2BB or a file hosted somewhere if the below returns the correct last row

VBA Code:
Sub FilterByMonth()
Dim lrow As Long

lrow = Cells.Find(What:="*", After:=Range("B7"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, MatchCase:=False).Row

MsgBox "The last row is  " & lrow

End Sub
 
Upvote 0
Then I'll definitely need the worksheet posted either with XL2BB or a file hosted somewhere if the below returns the correct last row

VBA Code:
Sub FilterByMonth()
Dim lrow As Long

lrow = Cells.Find(What:="*", After:=Range("B7"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, MatchCase:=False).Row

MsgBox "The last row is  " & lrow

End Sub
Yes, I am not getting the correct last row. I will try uploading worksheet.
 
Upvote 0
Then I'll definitely need the worksheet posted either with XL2BB or a file hosted somewhere if the below returns the correct last row

VBA Code:
Sub FilterByMonth()
Dim lrow As Long

lrow = Cells.Find(What:="*", After:=Range("B7"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, MatchCase:=False).Row

MsgBox "The last row is  " & lrow

End Sub
MARK858, thanks for help. After some frustration, I discovered the problem was in a column reference. I referenced the wrong one.
 
Upvote 0
MARK858, thanks for help. After some frustration, I discovered the problem was in a column reference. I referenced the wrong one.
Isn't that what I stated in the code line I posted where I changed the 5 to a 4? Don't know why you aren't just using A1 or the last cell in your lrow line though
 
Upvote 0

Forum statistics

Threads
1,223,625
Messages
6,173,392
Members
452,514
Latest member
cjkelly15

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top