Consulting

Results 1 to 4 of 4

Thread: VBA Date Filtering Issue

  1. #1

    VBA Date Filtering Issue

    I am using the below vba code to filter a spreadsheet on open to show the current month and all future dates and I can't for the life of me get it to show null values... where the date isn't populated. Any help would be appreciated!

    Private Sub Workbook_Open()
    Sheets("Sheet1").Select
    'startdate as date
    Dim startDate As Date
    'startdate as long for excel
    Dim lstartDate As Long
    'get start date, day = always 1
    startDate = DateSerial(Year(Now), Month(Now), 1)
    lstartDate = startDate
    'filter everything before current month
    'you see this month + future
    ActiveSheet.Range("A1").AutoFilter Field:=6, Criteria1:=">=" & lstartDate
    End Sub
    Last edited by Aussiebear; 03-25-2024 at 02:36 PM. Reason: Added code tags to supplied code

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Welcome to the forums, please take a minute to look at the FAQ in my signature

    I added CODE tags for you this time, you can use the [#] icon to insert them

    I think you're looking for the Bold below. The macro recorder usually does a reasonablely good job of giving you a starting point for syntax, but always requires cleaning up

    Private Sub Workbook_Open()
    Sheets("Sheet1").Select
    'startdate as date
    Dim startDate As Date
    'startdate as long for excel
    Dim lstartDate As Long
    'get start date, day = always 1
    startDate = DateSerial(Year(Now), Month(Now), 1)
    lstartDate = startDate
    'filter everything before current month
    'you see this month + future
    ActiveSheet.Range("A1").AutoFilter Field:=6, Criteria1:=">=" & lstartDate, Operator:=xlOr, Criteria2:="="
    End Sub
    https://learn.microsoft.com/en-us/of...nge.autofilter

    Parameters section has a good writeup
    Last edited by Paul_Hossler; 03-25-2024 at 08:15 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    To add blank cells to the same column criteria, update your Autofilter line:
    ActiveSheet.Range("A1").AutoFilter Field:=6, Criteria1:=">=" & lstartDate, Operator:=xlOr, Criteria2:="="
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  4. #4

    Thank you Paul and Jan

    This works perfectly, thank you!

Tags for this Thread

Posting Permissions

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