+ Reply to Thread
Results 1 to 17 of 17

Filter data based on two dates using ADODB

Hybrid View

Tejas.T Filter data based on two... 12-10-2012, 08:35 AM
mike7952 Re: Filter data based on two... 12-10-2012, 09:11 AM
event21 Re: Filter data based on two... 12-10-2012, 09:21 AM
Kyle123 Re: Filter data based on two... 12-10-2012, 09:31 AM
Fotis1991 Re: Filter data based on two... 12-10-2012, 09:31 AM
Tejas.T Re: Filter data based on two... 12-10-2012, 09:39 AM
Kyle123 Re: Filter data based on two... 12-10-2012, 09:44 AM
Tejas.T Re: Filter data based on two... 12-10-2012, 09:50 AM
mike7952 Re: Filter data based on two... 12-10-2012, 10:06 AM
mike7952 Re: Filter data based on two... 12-10-2012, 10:10 AM
Fotis1991 Re: Filter data based on two... 12-10-2012, 10:31 AM
Kyle123 Re: Filter data based on two... 12-10-2012, 10:41 AM
mike7952 Re: Filter data based on two... 12-10-2012, 11:08 AM
Fotis1991 Re: Filter data based on two... 12-10-2012, 11:18 AM
mike7952 Re: Filter data based on two... 12-10-2012, 11:24 AM
Fotis1991 Re: Filter data based on two... 12-10-2012, 11:35 AM
Tejas.T Re: Filter data based on two... 12-10-2012, 12:28 PM
  1. #1
    Forum Contributor
    Join Date
    10-21-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    110

    Filter data based on two dates using ADODB

    Hi All,

    I have written code to select data between two dates.

    But unfortunately it is not working when I select 1st date from May and 2nd date from June. If I give both the dates from May, it works.

    Please find the attached Macro file and have a look.

    Macro is activated on the click of the button in sheet called "Macro"

    Thanks in advance.

    Regards,
    Tejas
    Attached Files Attached Files
    Last edited by Tejas.T; 12-10-2012 at 09:52 AM.

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Filter data based on two dates using ADODB

    Works for me. Whats not working?
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Filter data based on two dates using ADODB

    Hi -

    Works for me too.

    Regards,
    Event

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Filter data based on two dates using ADODB

    More importantly, you shouldn't be querying a workbook like that. It causes a memory leak - if you want to use sql on a workbook, the workbook should not be the one in which the code resides - you need to create a workbook to do the querying and have one workbook with the data in

  5. #5
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Filter data based on two dates using ADODB

    @ mike & event.

    Does not work for me too. Row 8 of the first sheet is not copied in the new sheet.
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

  6. #6
    Forum Contributor
    Join Date
    10-21-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Filter data based on two dates using ADODB

    Hi,
    If you select 25-May-2012 as Date1 and 2-Jun-2012 as Date2, how many rows you are getting as result?
    I am getting only 4 rows and it is not fetching row with 01-06-2012 as ArrivalDate.

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Filter data based on two dates using ADODB

    The issue is your dates:

    Select * from [Data$] where [ArrivalDate] BETWEEN #25/05/2012# AND #02/06/2012# <> Select * from [Data$] where [ArrivalDate] BETWEEN #2012-05-25# AND #2012-06-02#
    SQL dates should always be passed as yyyy-mm-dd, try:

    Query = "Select * from [Data$] where [ArrivalDate] BETWEEN #" & Format(Range("B1"), "yyyy-mm-dd") & "# AND #" & Format(Range("B2"), "yyyy-mm-dd") & "#"
    But as I mentioned above, you shouldn't query a workbook like this

  8. #8
    Forum Contributor
    Join Date
    10-21-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Filter data based on two dates using ADODB

    WoW. It worked!!

    Thanks a lot.

    Well I have no other option. Client has instructed me to create a single file has deliverable.
    I can not give him two files, one as Data File and another as Macro File.

    Please suggest me if you have a better work-around for the same.

  9. #9
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Filter data based on two dates using ADODB

    You can try this

    Sub Macro1()
     Dim rngFilter As Range
     Dim sDate1 As String, sDate2 As String
     
     With Worksheets("macro")
        sDate1 = .Range("b1").Value
        sDate2 = .Range("b2").Value
     End With
     With Worksheets("data")
        .Range("$A$1").CurrentRegion.AutoFilter Field:=1, Criteria1:= _
            ">=" & sDate1, Operator:=xlAnd, Criteria2:="<=" & sDate2
        Set rngFilter = .Range("a1").CurrentRegion.SpecialCells(xlCellTypeVisible)
     End With
    
     If Not rngFilter Is Nothing Then
        With Worksheets("macro")
            rngFilter.Copy .Range("a6")
        End With
     End If
    End Sub

  10. #10
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Filter data based on two dates using ADODB

    @Fortis1991

    Ah, you are correct. I'm just on my first cup of coffee and not fully awake. Nice catch.

  11. #11
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Filter data based on two dates using ADODB

    ...@Fortis1991

    Ah, you are correct. I'm just on my first cup of coffee and not fully awake. Nice catch.
    I can see this Mike! Fotis ..not Fortis

    I have 2 questions here.

    1) Code is not working for me. Am i doing something wrong?

    2) What if we need one more condition. lET'S say that in macro sheet in C1, put another criterion. Let's say number 391.

    So result must be data with dates>=Macro!B1 AND <=Macro!B2 and =Macro!C1

    So the two yellows rows in first sheet must be the result.
    Attached Files Attached Files

  12. #12
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Filter data based on two dates using ADODB

    @Tejas, cross-posting is against the rules, if you have solved your problem, please post this wherever you have asked the question. It saves other people spending time helping yuo when you already have a solution

  13. #13
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Filter data based on two dates using ADODB

    @Fotis1991
    I can see this Mike! Fotis ..not Fortis
    Must be my Fat fingers... LOL This would include the ID filter. See attached wb.



    Sub Macro1()
     Dim rngFilter As Range
     Dim sDate1 As String, sDate2 As String
     Dim sID As String
     
     With Worksheets("macro")
        sDate1 = .Range("b1").Value
        sDate2 = .Range("b2").Value
        sID = .Range("c1").Value
     End With
     With Worksheets("data")
     
        .Range("$A$1").CurrentRegion.AutoFilter Field:=1, Criteria1:= _
            ">=" & sDate1, Operator:=xlAnd, Criteria2:="<=" & sDate2
        
        .Range("$A$1").CurrentRegion.AutoFilter Field:=2, Criteria1:=sID
        Set rngFilter = .Range("a2").CurrentRegion.SpecialCells(xlCellTypeVisible)
        
     End With
    
     If Not rngFilter Is Nothing Then
        With Worksheets("macro")
            .Range("a6:b" & .Cells(6, "b").End(xlDown).Row).ClearContents
            rngFilter.Copy .Range("a6")
        End With
     End If
    End Sub
    Attached Files Attached Files

  14. #14
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Filter data based on two dates using ADODB

    Must be something wrong in my PC. Same result!

    Never mind. Just was interesting for me and asked. Don't do effort for this.

  15. #15
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Filter data based on two dates using ADODB

    Just a quick suggestion try changing

        .Range("$A$1").CurrentRegion.AutoFilter Field:=1, Criteria1:= _
            ">=" & sDate1, Operator:=xlBetween, Criteria2:="<=" & sDate2
        
        .Range("$A$1").CurrentRegion.AutoFilter Field:=2, Criteria1:=sID
    to

        .Range("A1:B9").AutoFilter Field:=1, Criteria1:= _
            ">=" & sDate1, Operator:=xlBetween, Criteria2:="<=" & sDate2
        
        .Range("A1:B9").AutoFilter Field:=2, Criteria1:=sID

  16. #16
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Filter data based on two dates using ADODB

    Neither this!

  17. #17
    Forum Contributor
    Join Date
    10-21-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Filter data based on two dates using ADODB

    @Kyle123

    Sorry I posted it again because someone deleted my post by mistake. I got the message from Forum Admin and after that I posted it once again.

    BTW I am not able to count the number of rows returned.
    Can you please help me out how can I get the number of records returned by the query?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1