Sub ShowAll()
'show all filtered data and remove filter
With Sheet23
IfSheet23.AutoFilterMode Then
Sheet23.Range("B7").AutoFilter
End If
End With
End Sub
Sub ShowAllRecords()
'show data and keep filter
IfSheet23.FilterMode Then
Sheet23.ShowAllData
End If
'copy the filtered data
CopyFilter
End Sub
Sub CopyFilter()
'clear the contents
Sheet30.Range("B7:CK3007").ClearContents
'copy and paste the range
Sheet23.Range("Database").SpecialCells(xlCellTypeVisible).Copy_
Destination:=Sheet30.Range("B7")
End Sub
Sub Between2Dates()
'Sources: TrevorEaston
'http://www.onlinepclearning.com/auto-filter-dates-excel-vba/
'and Tom Urtis
'https://www.atlaspm.com/toms-tutorials-for-excel/toms-tutorials-for-excel-filtering-dates/
Dim StartDate As Date, EndDate As Date
Dim FilterStartDate As Date, FilterEndDate As Date
Dim FilterRange As Range
'Set error handler
On Error GoTo errHandler:
'Stop screen flicker
Application.ScreenUpdating = False
'Set the variables
StartDate = Sheet30.Range("G2").Value
EndDate = Sheet30.Range("H2").Value
'Check the dates if OK to run the filter
IfSheet30.Range("G2").Value >= Sheet30.Range("H2").ValueThen
MsgBox "Daterange invalid. Try again."
Exit Sub
Else
If NotIsEmpty(StartDate) And Not IsEmpty(EndDate) Then
Set FilterRange = Sheet23.Range("B7")
FilterStartDate = DateSerial(Year(StartDate),Month(StartDate), Day(StartDate))
FilterEndDate = DateSerial(Year(EndDate), Month(EndDate),Day(EndDate))
'Run filter
With FilterRange
.AutoFilterField:=1, Criteria1:=">=" & CDbl(FilterStartDate), _
Operator:=xlAnd, Criteria2:="<=" & CDbl(FilterEndDate)
'Copy values
CallCopyFilter
'Show all data
Call ShowAll
End With
End If
End If
'Error block
On Error GoTo 0
Exit Sub
errHandler:
MsgBox "There is no data"
Call ShowAllRecords
Sheet30.Select
End Sub