Consulting

Results 1 to 8 of 8

Thread: Autofilter: Filter between to dates

  1. #1

    Autofilter: Filter between to dates

    Hi all,

    I'm new on VBA and I'm trying to find a solution for my problem. So basically want I want to do is to filter all the data between two exact dates which I get from an inputbox. So I can only keep the payout dates from one week. At the beginning it worked fine but then I saw that I'll have problems if the dates are from two months. I also tried to do it with an Array but didn't worked. I really hope some one can help me as I'm now getting really frustrated
    Here is my beloved part of the macro and I also attach the worksheet.
    Thank you so much for helping me

    Sub filtertry()
    Dim EndDate0 As Date
    Dim EndDate As String
    Dim StartDate0 As Date
    Dim StartDate As String


    ActiveSheet.Name = "Settlement Overview"
    EndDate = InputBox("Please insert Enddate", Hi, Format(Now, "dd.mm.yyyy"))


    EndDate0 = CDate(EndDate)
    StartDate0 = EndDate0 - 4
    StartDate = Format(StartDate0, "dd.mm.yyyy")


    ActiveSheet.Range("A8").End(xlDown).End(xlToRight).Select


    Selection.AutoFilter Field:=4, Criteria1:=">=" & "StartDate", Operator:=xlAnd, Criteria2:="<=" & "EndDate", Operator:=xlFilterValues


    End Sub
    Attached Files Attached Files

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Try using this:

    Selection.AutoFilter Field:=4, Criteria1:=">=" & CDbl(StartDate0), Operator:=xlAnd, Criteria2:="<=" & CDbl(EndDate0), Operator:=xlAnd
    Be as you wish to seem

  3. #3
    Hi thanks for the quick answer. I tried it unfortunately it's also not working Could it be that I first have to convert the column in date format? I tried this but maybe had the wrong formula I just don't understand why it works if the the dates in the same month but as soon as you have a week within two months it's not working

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Those aren't real dates - they're text. You need to convert them to real dates before the filter will work.
    Be as you wish to seem

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    working with non-US dates in vba is always a pain.

    if column D contains real dates (not strings) below should work. it worked for the test file i used.


    Sub vbax_53986_filter_between_two_dates()
        Dim EndDate As Long
        
        EndDate = Application.InputBox("Please insert End Date", "END DATE", Format(Date, "dd.mm.yyyy"), , , , , 1)
        
        With ActiveSheet
            .Name = "Settlement Overview"
            .Range("A8").AutoFilter Field:=4, Criteria1:=">=" & EndDate - 4, Operator:=xlAnd, Criteria2:="<=" & EndDate
        End With
    End Sub

    edit:
    oppps. there was an attachment.
    as Aflatoon said, first convert strings in Col D to dates.
    Last edited by mancubus; 10-13-2015 at 08:00 AM. Reason: explanation added
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    Thanks guys, so I will try to convert them then

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Adapting Mancubus's code (after converting to dates (see your other thread)):
    Sub blah1()
    Dim EndDate
    EndDate = Application.InputBox("Please insert End Date", "END DATE", Format(Date, "dd/mm/yyyy"), , , , , 2)
    EndDate = CLng(CDate(EndDate))
    With ActiveSheet
      .Name = "Settlement Overview"
      .Range("A8").AutoFilter Field:=4, Criteria1:=">=" & EndDate - 4, Operator:=xlAnd, Criteria2:="<=" & EndDate
    End With
    End Sub
    From the -4 in the code I guess you're entering a Friday as the end date.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  8. #8
    Thank you so much for helping me

Posting Permissions

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