show all data if the two cells are empty

leap out

Active Member
Joined
Dec 4, 2020
Messages
271
Office Version
  1. 2016
  2. 2010
hello
I need help about this code to filter between two dates I would add condition if the k2,l2 are empty then show all the data
VBA Code:
Sub ff()
Dim StartDate As Date
Dim EndDate As Date
StartDate = Range("k2").Value
EndDate = Range("l2").Value
ActiveSheet.Range("A1:h1").AutoFilter Field:=1, Criteria1:=">=" & CDbl(StartDate), Operator:=xlAnd, Criteria2:="<=" & CDbl(EndDate)

End Sub
thanks in advance
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about
VBA Code:
Sub ff()
Dim StartDate As Date
Dim EndDate As Date
StartDate = Range("k2").Value
EndDate = Range("l2").Value
If StartDate = 0 Or EndDate = 0 Then
   ActiveSheet.Range("A1:h1").AutoFilter Field:=1
Else
   ActiveSheet.Range("A1:h1").AutoFilter Field:=1, Criteria1:=">=" & CDbl(StartDate), Operator:=xlAnd, Criteria2:="<=" & CDbl(EndDate)
End If
End Sub
 
Upvote 0
great! just I have request can you do that by worksheet change event I want when write or clear in two cells show the data without button
 
Upvote 0
Maybe
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim StartDate As Date
   Dim EndDate As Date
   
   If Target.CountLarge > 1 Then Exit Sub
   If Not Intersect(Target, Range("K2:L2")) Is Nothing Then
      If Target.Value = "" Then
         ActiveSheet.Range("A1:h1").AutoFilter Field:=1
      Else
         StartDate = Range("k2").Value
         EndDate = Range("l2").Value
         ActiveSheet.Range("A1:h1").AutoFilter Field:=1, Criteria1:=">=" & CDbl(StartDate), Operator:=xlAnd, Criteria2:="<=" & CDbl(EndDate)
      End If
   End If
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,497
Members
448,967
Latest member
visheshkotha

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