+ Reply to Thread
Results 1 to 3 of 3

Code not working the 2nd time in For loop

  1. #1
    Registered User
    Join Date
    05-15-2013
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007
    Posts
    32

    Code not working the 2nd time in For loop

    I have written a code with a for loop that filters certain data for a particular date. It runs perfectly for the first loop, however, in the second loop, it doesnt filter and instead hides all the data. Can somebody please explain to me why this is happening.

    For a = 5 To b

    Sheets("lists").Select
    e = Cells(a, 44)
    Cells(a, 44).Select

    k = Sheets("lists").Range("AW3") + 5


    If e > d Then
    Range("AR5").Select

    ElseIf e < d Or e = d Then
    Sheets("sample data").Select
    ActiveSheet.Range("$B$6:$T$500").AutoFilter Field:=1
    ActiveSheet.Range("$B$6:$T$500").AutoFilter Field:=1, Criteria1:="=" & e
    Range("I6").Select

    This is the section of the code that is not working correctly. 'e' & 'd' are dates.

  2. #2
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: Code not working the 2nd time in For loop

    have you checked that e is in the same format as the dates in that column?
    it would be easier if you supplied your full code or better still an example workbook.
    Regards
    Sean

    Please add to my reputation if you think i helped
    (click on the star below the post)
    Mark threads as "Solved" if you have your answer
    (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code:
    [code] Your code here [code]
    Please supply a workbook containing example Data:
    It makes its easier to answer your problem & saves time!

  3. #3
    Registered User
    Join Date
    05-15-2013
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Code not working the 2nd time in For loop

    I changed my code to -

    ActiveSheet.Range("$B$6:$T$500").AutoFilter Field:=1, Criteria1:=">=" & e - 1, Operator:=xlAnd, Criteria2:="<=" & e + 1, Operator:=xlFilterValues

    Its working fine now. Still dont know why it wasnt earlier. The format was the same. But its ok. I'm sorted for now. Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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