Search by Date VBA code help

ExcelGirl1988

New Member
Joined
Mar 27, 2017
Messages
44
Hi, I hope someone can help with this, I am not sure what the issue is with my code. I have a complaints spreadsheet with a search by date option but this code is not working as it should. When I am searching for complaints between 01/04/2021 and 31/03/2022 it should find 20 complaints but instead it brings up 36, it is showing the complaints outside of the dates I am wanting. I hope someone can figure out where I made an error with my code, I have copied it below.
VBA Code:
Option Explicit
Sub ExtractDataBasedOnDate_2()
    
    Dim erow As Long, i As Long, instances As Long, lastrow As Long
    Dim myDate As Date, StartDate As Date, EndDate As Date
    Dim ws As Worksheet, wsSummary As Worksheet, sht As Worksheet
    Dim answer As VbMsgBoxResult
    
    Set wsSummary = ThisWorkbook.Worksheets("Summary")
    
    Application.ScreenUpdating = False
    
    With Worksheets("Home")
        StartDate = CDate(.Range("B1").Value)
        EndDate = CDate(.Range("B2").Value)
    End With
    
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Summary" And ws.Name <> "Home" And ws.Name <> "Data" Then
            Application.CutCopyMode = False
            For i = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
            Application.CutCopyMode = False
                myDate = ws.Cells(i, 2)
                If myDate >= StartDate And myDate <= EndDate Then
                    erow = wsSummary.Cells(wsSummary.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                    ws.Cells(i, 1).Resize(i, ws.Cells(i, ws.Columns.Count).End(xlToLeft).Column).Copy _
                    Destination:=wsSummary.Cells(erow, 1)
                    Application.CutCopyMode = False
                End If
            Next i
        End If
     Next ws

With ThisWorkbook.Worksheets("Summary").Sort
    .SortFields.Clear
     .SortFields.Add Key:=Range("B1"), Order:=xlAscending
     .SetRange Range("A:O")
     .Header = xlYes
     .Apply
End With

Set sht = ThisWorkbook.Worksheets("Home")
wsSummary.Range("A:O").RemoveDuplicates Columns:=Array(1, 4), Header:=xlYes
Application.ScreenUpdating = True

lastrow = wsSummary.Cells(Rows.Count, "A").End(xlUp).Row - 1

If lastrow = 0 Then
MsgBox "No Complaints Found", , "Search Complete"
Else
answer = MsgBox("There are " & lastrow & " complaints found" & vbNewLine & _
    "Go to Summary sheet now?", vbYesNo, "Search Complete")
If answer = vbYes Then
wsSummary.Activate
Else

End If
End If

End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
There are a few clues that lead me to believe I think I know what the problem might be.
First, you mention this:
When I am searching for complaints between 01/04/2021 and 31/03/2022
and then in your code, I see this:
VBA Code:
    With Worksheets("Home")
        StartDate = CDate(.Range("B1").Value)
        EndDate = CDate(.Range("B2").Value)
    End With
This leads me to believe that the dates entered into cells B1 and B2 are actually entered as Text, and not as valid Dates (or else you would not need to use the "CDate" function on them).

And from the structure of your dates (i.e. "31/03/2022"), I am guessing that you are using a European date format.

However, the issue is that Excel VBA uses US Date formatting. Hence, it reads "01/04/2021" as "January 4" instead of "April 1". And that will mess up all your comparisons.

Is there any chance you can change the entries into cells B1 and B2 so that either:
1. They are entered as valid dates and not text (and then you wouldn't need to use "CDate");
- or -
2. The text entry can use month name or abbreviation (i.e. "Jan" or "January") instead of a numeric representation of the month?

If you can do either of those things, I think that will fix your issues. If not, you will need to do something else in VBA code to try to account for it (i.e. string manipulation or something like that).
I am in the US, so I cannot recreate that situation myself, but I have seen this issue come up many times for those working in Europe.
 
Upvote 0
Hi Joe

Thank you for your response. I have tried to change the date boxes to a date format and removed CDate from the VBA code but this didn't work so I then changed the date format to use the month name but this didn't work either. I am not sure what the issue could be. Although, I think it possibly could be something to do with the search just adding onto the previous search, is that a thing?
 
Upvote 0
How are the values in B1 and B2 being populated?

Note that changing the cell format of a cell AFTER the data has already been entered will NOT change the entry. The data has to be re-entered.
If you change the format of cells B1 and B2 to some date format, and then re-enter the values, does it work then?
What EXACTLY are you entering in cells B1 and B2 (please show us the values being entered)?
 
Upvote 0
The values are entered into cells B1 and B2 manually by the user. I deleted the dates in the cells, updated the cell format to Date and then re-entered the dates but the search is still finding too many entries. I have attached a screenshot of what is happening.
 

Attachments

  • ExcelScreenshot.png
    ExcelScreenshot.png
    146.2 KB · Views: 14
Upvote 0
Can you post some sample data from your sheets you are counting from so we can see what it looks like?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Hi, here some sample data that I am using with the date search :) Let me know if you need any further data.

Calderdale Schools Complaints Log Remake v2.xlsm
ABCDEFGHIJKLMNO
1School NameDateSource of ComplaintOfsted ReferenceOfsted CategoryComplaints Log (Word)ComplainantComplaint Sent toTo be Dealt With byLado (Y/N)Response to Oftsed Due ByOftsed ReferralComplainant DetailsResponse sent to OfstedStatus
2Lightcliffe Academy28/10/2021Ofsted CAS-4577873T:\Safeguarding & Quality Assurance\Shared\School Complaints (Ofsted, NSPCC, MP etc)\Ofsted Complaints 2021-2022\Lightcliffe Academy\CAS - 457787\CAS - 457787.doc Ms Sophia ZaccardelliCheryl BaxterSteve BarnesNo25/11/2021YesYes06/01/2021C
3Lightcliffe Academy26/07/2022Ofsted CAS-4728113T:\Safeguarding & Quality Assurance\Shared\School Complaints (Ofsted, NSPCC, MP etc)\Ofsted Complaints 2022-2023\Lightcliffe Academy\CAS 472811\Ofsted Complaints Record CAS-472811.doc AnonymousCheryl BaxterConnie BeirneNo23/08/2022YesNo30/09/2022C
4Lightcliffe Academy09/12/2022OfstedCAS-4789423T:\Safeguarding & Quality Assurance\Shared\School Complaints (Ofsted, NSPCC, MP etc)\Ofsted Complaints 2022-2023\Lightcliffe Academy\CAS-478942\Ofsted Complaints Record CAS-478942.docLindsey MullaneyJulie JenkinsSteve BarnesNo06/01/2023YesYes17/02/2023C
5Lightcliffe Academy16/12/2022OfstedCAS-4794563\\hpnode2\cyp\CYPSTeams\Safeguarding & Quality Assurance\Shared\School Complaints (Ofsted, NSPCC, MP etc)\Ofsted Complaints 2022-2023\Lightcliffe Academy\CAS 479456\Ofsted Complaints Record - Lightcliffe Academy CAS 479456.docAnonymousCheryl BaxterSteve BarnesNo16/01/2023YesNo27/01/2023C
6Lightcliffe Academy08/03/2023Ofsted CAS-4849533\\hpnode2\cyp\CYPSTeams\Safeguarding & Quality Assurance\Shared\School Complaints (Ofsted, NSPCC, MP etc)\Ofsted Complaints 2022-2023\Lightcliffe Academy\CAS-484953\Ofsted Complaint CAS-484953.doc Sylvia MullaneyCheryl BaxterSteve BarnesNo05/04/2023YesYesP
7Lightcliffe Academy28/03/2023Ofsted CAS-48671603-Jan\\hpnode2\cyp\CYPSTeams\Safeguarding & Quality Assurance\Shared\School Complaints (Ofsted, NSPCC, MP etc)\Ofsted Complaints 2022-2023\Lightcliffe Academy\CAS-486716\CAS- 486716.docx AnonymousCheryl BaxterSteve BarnesNo25/04/2023YesNoP
Lightcliffe Academy
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:O100Expression=$O2 = "N"textNO
A2:O100Expression=$O2 = "P"textNO
A2:O100Expression=$O2 = "C"textNO
Cells with Data Validation
CellAllowCriteria
L2:M7ListYes, No
 
Upvote 0
Are your entries in column D actual date entries or text entries?
By default, text entries are usually left-justified in cells, while valid date and numeric entries are right-justified.
If you have text entries, you will need to convert them to valid date entries first.

The easiest way to do that is to select column B, go to the Data menu and select "Text to Columns".
Then, click "Next" until you get to Step 3. Then choose the Date radio button and select the DMY date format option and click Finish.

(So whenever doing date comparisons, you need to compare dates-to-dates. If either are entered as text, you won't get accurate results).
 
Upvote 0
I have converted all my column Bs in the workbook to Dates using the "Text to Columns" method but the search is still pulling up entries for dates outside the dates I have entered into the search. I have tried using different dates in the search and it is not working. For some reason the search is pulling through random entries that are outside the search dates. I have attached another minisheet as an example.

Calderdale Schools Complaints Log Remake v2.xlsm
ABCDEFGH
1Start Date20/06/2020Summary
2End Date23/06/2020
3
4School Name2020/2021 Complaints2021/2022 Complaints2022/2023 Complaints2023/2024 Complaints
5Abbey Park Academy0000
6All Saints' CofE Primary School0000
7Ash Green Community Primary School0000
8Bailiffe Bridge Junior and Infant School0000
9Barkisland CofE VA Primary School0000
10Beech Hill School0000
11Bolton Brow Primary Academy0000
12Bowling Green Academy0000
13Bradshaw Primary School0000
14Brighouse High School1010
Home
Cell Formulas
RangeFormula
C5C5=COUNTIFS('Abbey Park Academy'!$B$2:$B$109, ">=01/04/2020", 'Abbey Park Academy'!$B$2:$B$109, "<=31/3/2021")
D5D5=COUNTIFS('Abbey Park Academy'!B2:B109, ">=01/04/2021", 'Abbey Park Academy'!B2:B109, "<=31/03/2022")
E5E5=COUNTIFS('Abbey Park Academy'!B2:B109, ">=01/04/2022", 'Abbey Park Academy'!B2:B109, "<=31/03/2023")
F5F5=COUNTIFS('Abbey Park Academy'!C2:C109, ">=01/04/2023", 'Abbey Park Academy'!C2:C109, "<=31/03/2024")
C6C6=COUNTIFS('All Saints CofE Primary'!B2:B113, ">=01/04/2020", 'All Saints CofE Primary'!B2:B113, "<=31/3/2021")
D6D6=COUNTIFS('All Saints CofE Primary'!$B$2:$B$133, ">=1/4/2021", 'All Saints CofE Primary'!$B$2:$B$133, "<=31/3/2022")
E6E6=COUNTIFS('All Saints CofE Primary'!$B$2:$B$133, ">=1/4/2022", 'All Saints CofE Primary'!$B$2:$B$133, "<=31/3/2023")
F6F6=COUNTIFS('All Saints CofE Primary'!$B$2:$B$133, ">=1/4/2023", 'All Saints CofE Primary'!$B$2:$B$133, "<=31/3/2024")
C7C7=COUNTIFS('Ash Green Community Primary'!B2:B114, ">=01/04/2020", 'Ash Green Community Primary'!B2:B114, "<=31/3/2021")
D7D7=COUNTIFS('Ash Green Community Primary'!$B$2:$B$133, ">=1/4/2021", 'Ash Green Community Primary'!$B$2:$B$133, "<=31/3/2022")
E7E7=COUNTIFS('Ash Green Community Primary'!$B$2:$B$133, ">=1/4/2022", 'Ash Green Community Primary'!$B$2:$B$133, "<=31/3/2023")
F7F7=COUNTIFS('Ash Green Community Primary'!$B$2:$B$133, ">=1/4/2023", 'Ash Green Community Primary'!$B$2:$B$133, "<=31/3/2024")
C8C8=COUNTIFS('Bailiffe Bridge J&I'!B2:B115, ">=1/4/2020", 'Bailiffe Bridge J&I'!B2:B115, "<=31/3/2021")
D8D8=COUNTIFS('Bailiffe Bridge J&I'!$B$2:$B$133, ">=1/4/2021", 'Bailiffe Bridge J&I'!$B$2:$B$133, "<=31/3/2022")
E8E8=COUNTIFS('Bailiffe Bridge J&I'!$B$2:$B$133, ">=1/4/2022", 'Bailiffe Bridge J&I'!$B$2:$B$133, "<=31/3/2023")
F8F8=COUNTIFS('Bailiffe Bridge J&I'!$B$2:$B$133, ">=1/4/2023", 'Bailiffe Bridge J&I'!$B$2:$B$133, "<=31/3/2024")
C9C9=COUNTIFS('Barkisland CofE'!B2:B116, ">=01/04/2020", 'Barkisland CofE'!B2:B116, "<=31/3/2021")
D9D9=COUNTIFS('Barkisland CofE'!$B$2:$B$133, ">=1/4/2021", 'Barkisland CofE'!$B$2:$B$133, "<=31/3/2022")
E9E9=COUNTIFS('Barkisland CofE'!$B$2:$B$133, ">=1/4/2022", 'Barkisland CofE'!$B$2:$B$133, "<=31/3/2023")
F9F9=COUNTIFS('Barkisland CofE'!$B$2:$B$133, ">=1/4/2023", 'Barkisland CofE'!$B$2:$B$133, "<=31/3/2024")
C10C10=COUNTIFS('Beech Hill'!B2:B117, ">=01/04/2020", 'Beech Hill'!B2:B117, "<=31/3/2021")
D10D10=COUNTIFS('Beech Hill'!$B$2:$B$133, ">=1/4/2021", 'Beech Hill'!$B$2:$B$133, "<=31/3/2022")
E10E10=COUNTIFS('Beech Hill'!$B$2:$B$133, ">=1/4/2022", 'Beech Hill'!$B$2:$B$133, "<=31/3/2023")
F10F10=COUNTIFS('Beech Hill'!$B$2:$B$133, ">=1/4/2023", 'Beech Hill'!$B$2:$B$133, "<=31/3/2024")
C11C11=COUNTIFS('Bolton Brow Primary'!B2:B118, ">=01/04/2020", 'Bolton Brow Primary'!B2:B118, "<=31/3/2021")
D11D11=COUNTIFS('Bolton Brow Primary'!$B$2:$B$133, ">=1/4/2021", 'Bolton Brow Primary'!$B$2:$B$133, "<=31/3/2022")
E11E11=COUNTIFS('Bolton Brow Primary'!$B$2:$B$133, ">=1/4/2022", 'Bolton Brow Primary'!$B$2:$B$133, "<=31/3/2023")
F11F11=COUNTIFS('Bolton Brow Primary'!$B$2:$B$133, ">=1/4/2023", 'Bolton Brow Primary'!$B$2:$B$133, "<=31/3/2024")
C12C12=COUNTIFS('Bowling Green Academy'!B2:B119, ">=01/04/2020", 'Bowling Green Academy'!B2:B119, "<=31/3/2021")
D12D12=COUNTIFS('Bowling Green Academy'!$B$2:$B$133, ">=1/4/2021", 'Bowling Green Academy'!$B$2:$B$133, "<=31/3/2022")
E12E12=COUNTIFS('Bowling Green Academy'!$B$2:$B$133, ">=1/4/2022", 'Bowling Green Academy'!$B$2:$B$133, "<=31/3/2023")
F12F12=COUNTIFS('Bowling Green Academy'!$B$2:$B$133, ">=1/4/2023", 'Bowling Green Academy'!$B$2:$B$133, "<=31/3/2024")
C13C13=COUNTIFS('Bradshaw Primary'!B2:B120, ">=01/04/2020", 'Bradshaw Primary'!B2:B120, "<=31/3/2021")
D13D13=COUNTIFS('Bradshaw Primary'!$B$2:$B$133, ">=1/4/2021", 'Bradshaw Primary'!$B$2:$B$133, "<=31/3/2022")
E13E13=COUNTIFS('Bradshaw Primary'!$B$2:$B$133, ">=1/4/2022", 'Bradshaw Primary'!$B$2:$B$133, "<=31/3/2023")
F13F13=COUNTIFS('Bradshaw Primary'!$B$2:$B$133, ">=1/4/2023", 'Bradshaw Primary'!$B$2:$B$133, "<=31/3/2024")
C14C14=COUNTIFS('Brighouse High'!B2:B121, ">=01/04/2020", 'Brighouse High'!B2:B121, "<=31/3/2021")
D14D14=COUNTIFS('Brighouse High'!$B$2:$B$133, ">=1/4/2021", 'Brighouse High'!$B$2:$B$133, "<=31/3/2022")
E14E14=COUNTIFS('Brighouse High'!$B$2:$B$133, ">=1/4/2022", 'Brighouse High'!$B$2:$B$133, "<=31/3/2023")
F14F14=COUNTIFS('Brighouse High'!$B$2:$B$133, ">=1/4/2023", 'Brighouse High'!$B$2:$B$133, "<=31/3/2024")
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A5:F105Expression=$C5>0textNO
A5:F105Expression=$C5>4textNO
A5:F105Expression=$C5>9textNO
A5:F105Expression=$F5>9textNO
A5:F105Expression=$F5>4textNO
A5:F105Expression=$F5>0textNO
A5:F105Expression=$E5>9textNO
A5:F105Expression=$E5>4textNO
A5:F105Expression=$E5>0textNO
A5:F105Expression=$D5>9textNO
A5:F105Expression=$D5>4textNO
A5:F105Expression=$D5>0textNO


Calderdale Schools Complaints Log Remake v2.xlsm
ABCDEFGHIJKLMNOP
1School NameDateSource of ComplaintOfsted ReferenceOfsted CategoryComplaints Log (Word)ComplainantComplaint Sent toTo be Dealt With byLado (Y/N)Response to Oftsed Due ByOftsed ReferralComplainant DetailsResponse sent to OfstedStatusHome
2Brighouse High22/06/2020OfstedCAS-4404452\\hpnode2\cyp\CYPSTeams\Safeguarding & Quality Assurance\Shared\School Complaints (Ofsted, NSPCC, MP etc)\Ofsted Complaints 2020-2021\Brighouse High\Ofsted complaint ref CAS Ref 440445.msgAnonymousLADO MailboxSteve BarnesYesYesNo06/07/2020C
3Brighouse High13/01/2023Ofsted CAS-4806303\\hpnode2\cyp\CYPSTeams\Safeguarding & Quality Assurance\Shared\School Complaints (Ofsted, NSPCC, MP etc)\Ofsted Complaints 2022-2023\Brighouse High School\CAS-480630\Complaint Record CAS-480630 Brighouse High School.doc Lisa StopfordCheryl BaxterSteve BarnesNo10/02/2023YesYes16/02/2023C
Summary
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:O3Expression=$O2 = "N"textNO
A2:O3Expression=$O2 = "P"textNO
A2:O3Expression=$O2 = "C"textNO
Cells with Data Validation
CellAllowCriteria
L2:M3ListYes, No
 
Upvote 0
I don't know that I can analyze it any further without access to your file.
If you are able to upload a copy to a file sharing site and provide the link here, I can take a look at it later today when I have access to download files.
Be sure to "dummy up" any sensitive data, and provide an example with details that shows your expected values and your actual values.
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,595
Members
449,089
Latest member
Motoracer88

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