xlfilteralldatesinperiod"variable" not working

noidea23

New Member
Joined
Feb 16, 2022
Messages
28
Office Version
  1. 2021
Platform
  1. Windows
Sub testing()
a = InputBox("Month: ")

Sheets("Sheet1").ListObjects("SummaryTable").Range.AutoFilter Field:=4, Criteria1:=xlFilterAllDatesInPeriod"a", _
Operator:=xlFilterDynamic


Hello, I need help on how to filter data based on user input month. The month in the database is in the format e.g. 31/01/2017
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi
welcome to forum

See if this update to your code does what you want

VBA Code:
Sub testing()
    Dim xlMonth     As Variant
    Do
        xlMonth = InputBox("Enter Month name: ", "Enter Month Name")
        'cancel pressed
        If StrPtr(xlMonth) = 0 Then Exit Sub
        On Error Resume Next
        xlMonth = Month(DateValue(xlMonth & "/" & Year(Date)))
    Loop Until Err = 0
   
    xlMonth = CLng(xlMonth) + 20
   
    Sheets("Sheet1").ListObjects("SummaryTable").Range.AutoFilter Field:=4, Criteria1:=xlMonth, _
                                                     Operator:=xlFilterDynamic
End Sub

Dave
 
Upvote 0
Hi
welcome to forum

See if this update to your code does what you want

VBA Code:
Sub testing()
    Dim xlMonth     As Variant
    Do
        xlMonth = InputBox("Enter Month name: ", "Enter Month Name")
        'cancel pressed
        If StrPtr(xlMonth) = 0 Then Exit Sub
        On Error Resume Next
        xlMonth = Month(DateValue(xlMonth & "/" & Year(Date)))
    Loop Until Err = 0
  
    xlMonth = CLng(xlMonth) + 20
  
    Sheets("Sheet1").ListObjects("SummaryTable").Range.AutoFilter Field:=4, Criteria1:=xlMonth, _
                                                     Operator:=xlFilterDynamic
End Sub

Dave
it works! Thank you so much :biggrin:
 
Upvote 0
Hii, currently vba is reading my input dates (i.e., 28/2/2021) in the month-day-year format
I've tried modifying the line below to include Day(Date) but doesn't seem to be working:

xlMonth = Month(DateValue(xlMonth & "/" & Year(Date)))

Also, if im intending to filter by Year (from user input also) then Month, how should I go about doing it concurrently?
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

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