Date in textbox

macangc

Board Regular
Joined
Mar 28, 2006
Messages
140
Office Version
  1. 365
Hi All

I have 2 textbox's that drive a custom filter. The user enters a particular date in each of the textbox's which then filter the worksheet. Is there a way they can enter the month as well so if they want a full month then they can input June and June rather than 01/06/2011 and 30/06/2011. Current code below

Thanks

Private Sub Insertcmdbutton_Click()

With Worksheets("Service Ticket Detail").Columns("A:M")
.AutoFilter
.AutoFilter Field:=9, Criteria1:=">=" & CLng(DateValue(FirstMonth.Value)), Operator:=xlAnd, Criteria2:="<=" & CLng(DateValue(LastMonth.Value))
If FaultCat.Value <> "ALL" Then
.AutoFilter Field:=3, Criteria1:="=" & FaultCat.Value
End If
End With
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try this code:

Now Just type month name in the two boxes

Code:
Private Sub cmdSubmit_Click()
Dim dteFirstDate    As Date
Dim dteLastDate     As Date
Dim i As Long, j As Long, k As Long
On Error Resume Next
For i = 1 To 12
 If Left$(MonthName(i), 3) = Left$(FirstDate, 3) Then
    'dteFirstDate = Format("01/" & FirstDate.Value & "/" & Format(Date, "yyyy"), "mm/dd/yyyy")
      j = 1
 End If
 
 If Left$(MonthName(i), 3) = Left$(LastDate, 3) Then
    dteLastDate = Format("01/" & LastDate.Value & "/" & Format(Date, "yyyy"), "mm/dd/yyyy")
    k = 1
 
 End If
Next

dteLastDate = Format("01/" & LastDate.Value & "/" & Format(Date, "yyyy"), "mm/dd/yyyy")
dteLastDate = Format(DateSerial(Year(dteLastDate), Month(dteLastDate) + 1, 1) - 1, "dd/mm/yyyy")
 If j = 1 And k = 1 And IsDate(dteLastDate) >= IsDate(dteFirstDate) Then
    With Worksheets("Service Ticket Detail").Columns("A:M")
      .AutoFilter
      .AutoFilter Field:=9, Criteria1:=">=" & dteFirstDate, Operator:=xlAnd, Criteria2:="<=" & dteLastDate
      If FaultCat.Value <> "ALL" Then
        .AutoFilter Field:=3, Criteria1:="=" & FaultCat.Value
      End If
    End With
 Else
X:
 MsgBox "Either spelling of entered month is wrong or Last Month is less than First Month.!!"
 End If
 On Error GoTo 0
End Sub
 
Upvote 0
Thanks for getting back to me

When I overwrite my current code with your suggested code i seem to always get the message

"Either spelling of entered month is wrong or Last Month is less than First Month.!!"

Im not sure why, but i do have a 3rd combobox (listing fault categories) which is also part of the filter, could that be the problem?

cheers
 
Upvote 0
In firstdate text box and LastDate text box will now only contain the Months example "June" and "June". If you will enter any abserd text in these text boxes or if you will enter LastMonth lesser than First month in these cases only you will get this error.

Are you making any mistake like this?
 
Upvote 0
It was the textbox name, I had changed it to firstmonth and lastmonth so just changed it back to match your code. However now the filter for the date doesnt seem to work and shows nothing. When I look at what it has filtered by it shows as

is after or equal to 00/01/1900
is before or equal to 31/01/2011

I entered June in both textboxes so dont know why im getting this
 
Upvote 0
use this One:
On top of your code window include "Option Compare Text"

Code:
Option Compare Text
Private Sub cmdSubmit_Click()
Dim dteFirstMonth    As Date
Dim dteLastMonth     As Date
Dim i As Long, j As Long, k As Long
On Error Resume Next
For i = 1 To 12
 If Left$(MonthName(i), 3) = Left$(FirstMonth, 3) Then
    'dteFirstMonth = Format("01/" & FirstMonth.Value & "/" & Format(Date, "yyyy"), "mm/dd/yyyy")
      j = 1
 End If
 
 If Left$(MonthName(i), 3) = Left$(LastMonth, 3) Then
    dteLastMonth = Format("01/" & LastMonth.Value & "/" & Format(Date, "yyyy"), "mm/dd/yyyy")
    k = 1
 
 End If
Next
dteLastMonth = Format("01/" & LastMonth.Value & "/" & Format(Date, "yyyy"), "mm/dd/yyyy")
dteLastMonth = Format(DateSerial(Year(dteLastMonth), Month(dteLastMonth) + 1, 1) - 1, "dd/mm/yyyy")
 If j = 1 And k = 1 And IsDate(dteLastMonth) >= IsDate(dteFirstMonth) Then
    With Worksheets("Service Ticket Detail").Columns("A:M")
      .AutoFilter
      .AutoFilter Field:=9, Criteria1:=">=" & dteFirstMonth, Operator:=xlAnd, Criteria2:="<=" & dteLastMonth
      If FaultCat.Value <> "ALL" Then
        .AutoFilter Field:=3, Criteria1:="=" & FaultCat.Value
      End If
    End With
 Else
X:
 MsgBox "Either spelling of entered month is wrong or Last Month is less than First Month.!!"
 End If
 On Error GoTo 0
 
End Sub
 
Upvote 0
Same result im afraid, copied your code including the "option compare text" but still not filtering

thanks
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

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