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
 
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.
Hi, thank you. The link to my file is below. The date search is still not working as it should, I have added comments on the spreadsheet regarding what is happening. Since taking out the confidential info in rows D-H the date search is finding less complaints rather than more than it should. Really weird.
Dummy Complaints Log
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Your link is asking for a password. Can you make it available to anyone who has the link.
I am login off for the night soon but it sounds like @Joe4 will be trying your file later.
 
Upvote 0
Hi, thank you. The link to my file is below. The date search is still not working as it should, I have added comments on the spreadsheet regarding what is happening. Since taking out the confidential info in rows D-H the date search is finding less complaints rather than more than it should. Really weird.
Dummy Complaints Log
Dummy Complaints Log I have edited the permissions so it should be able to be accessed without a password.
 
Upvote 0
I don't think you get this screen if you set it up so others can see the file based on the link

1681388636693.png
 
Upvote 0
Alex,

Are you using a European version of Excel? I was hoping that someone who does would weigh in, as if the issue with the "American dates in VBA" is part of the problem for people using European versions of Excel, I cannot really rest that part of it.
 
Upvote 0
It's not the dates that is causing the problem.
This line
VBA Code:
                    ws.Cells(i, 1).Resize(i, ws.Cells(i, ws.Columns.Count).End(xlToLeft).Column).Copy _
should be Resize(1, not i
And remove this line
VBA Code:
wsSummary.Range("A:O").RemoveDuplicates Columns:=Array(1, 4), Header:=xlYes
 
Upvote 1
Solution
It's not the dates that is causing the problem.
This line
VBA Code:
                    ws.Cells(i, 1).Resize(i, ws.Cells(i, ws.Columns.Count).End(xlToLeft).Column).Copy _
should be Resize(1, not i
And remove this line
VBA Code:
wsSummary.Range("A:O").RemoveDuplicates Columns:=Array(1, 4), Header:=xlYes
OMG I can’t believe it, that worked a treat, thank you! I have been going round in circles for days thinking that there was something wrong with the dates.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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