NorthbyNorthwest
Board Regular
- Joined
- Oct 27, 2013
- Messages
- 173
- Office Version
- 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.
Thanks,
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,