Results 1 to 4 of 4

Thread: Date COlumn AutoFiltering Issue!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Date COlumn AutoFiltering Issue!

    Hi All,

    I have been trying to resolve the following issue which is driving me crazy! In a nutshell, I am trying to autofilter a date column based on a date range (from, to) which is provided by users from main menu. When I use string varibales to pass these dates to the FilterDate sub, I get wrong results but if I hard code the date range, I get good results. Below are more details....

    I have a VBA sub which is use to filter out my data in a seperate worksheet as follows:
    Code:
    Private Sub FilterData(bRemoveExistingFilter As Boolean, _
                                   Sht As Worksheet, _
                                    Rng As Range, _
                                    iField As Integer, _
                                    Optional vCriteria1 As Variant, _
                                    Optional vCriteria2 As Variant)
        Sht.Select
        With Sht
            'Remove any existing filters
            If (bRemoveExistingFilter) Then
                Rng.AutoFilter
            End If
    
            If IsMissing(vCriteria1) Then
                Rng.AutoFilter Field:=iField
            Else
                If (IsMissing(vCriteria2)) Then 
                      Rng.AutoFilter Field:=iField, Criteria1:=vCriteria1, Operator:=xlFilterValues
                Else
                      Rng.AutoFilter Field:=iField, Criteria1:=vCriteria1, Operator:=xlAnd, Criteria2:=vCriteria2
                End If
            End If
        End With
    End Sub

    The sub is working. The problem is that if I call this sub using my program variables (sReportingFromDate, and sReportingToDate) as below, the target date column is not filtered correctly and I get wrong results:

    Call FilterData(False, oData, oData.Range("A2").CurrentRegion, iDatesFilteringColumn, ">=" & sReportingFromDate, "<=" & sReportingToDate)

    The problem seems to be the ">=" & sReportingFromDate, "<=" & sReportingToDate" part of the above call. The sReportingFromDate and sReportingToDate are STRING variables which users pass the date range they need to do reporting.

    If I call the sub using hard coded dates like below, I get the correct result:
    Call FilterData(False, oData, oData.Range("A2").CurrentRegion, iDatesFilteringColumn, ">=11/1/2013", "<=11/30/2013")

    Can you please help me resolve this issue which is preventing me from moving forward with the rest of my program logic? thanks!
    Last edited by labkhand; 11-05-2013 at 01:30 AM.

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Use DATEVALUE

    DateValue(sReportingFromDate)
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0
    Thanks for your response, but I get "Type Mismatch" error when I use datevalue function since the following line in my FIlterData sub expects Variant values. Additionally, I use this sub in other places to filter data columns based on passed Variant/Array values and thus cannot change the sub declaration to anything else.

    Rng.AutoFilter Field:=iField, Criteria1:=vCriteria1, Operator:=sOperator, Criteria2:=vCriteria2

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0
    I was able to resolve this issue using the solution you provided. Thanks for all your help.

Similar Threads

  1. AutoFiltering Data COlumns
    By labkhand in forum Excel Help
    Replies: 2
    Last Post: 10-17-2013, 06:01 AM
  2. Replies: 4
    Last Post: 08-20-2013, 06:28 PM
  3. Replies: 5
    Last Post: 06-15-2013, 12:40 PM
  4. Help- Locking column basis current date.
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 1
    Last Post: 03-25-2013, 04:44 PM
  5. SharePoint Hyperlink issue
    By PeteRooney in forum Excel Help
    Replies: 0
    Last Post: 10-30-2012, 03:25 PM

Posting Permissions

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