Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Do Not AutoFilter If Data Does Not Exist

  1. #1

    Do Not AutoFilter If Data Does Not Exist

    Hello ExcelFox Forum users. My first post, how exciting.

    OK well I am a beginner i'm afraid so I hope I don't dumb down the water of this site too much

    I don't work with code but at work I have created many spread sheets in order to keep a record of international shipments coming into the UK.
    I have written many Macros and formulas on these spreadsheets but Im just having one problem I cant seem to get on top of. I would be very grateful if you kind people would help me.

    My spreadsheet has a column which is shows a date. This date is when the container delivered to the customer in UK. I run a macro triggered by a button which filters the column to the current date. Therefore when run on the date of delivery is shows "Deliveries for today" This is fine but when there are no deliveries that day i.e. today's date is not in any cell in this specific column then it filters and nothing is displayed. Because the spreadsheet goes out to the customer I think this looks a little messy. Instead, if there are no deliveries on the day I would like the 'msgbox' to pop up and say "there are no deliveries today" instead of just filtering so it just shows empty, non active rows. I have tries many bits of code over the last few weeks but can't get any to work.

    My code for the filter is below. I need perhaps an 'If' statement so if after filter this is no active rows then msgbox "No deliveries today"

    Code:
    Sub todaysdels()
    
        dDate = Date
        
        Range("A2").AutoFilter field:=20, Criteria1:=dDate
        
    End Sub
    I hope I have explained correctly. Thanks.
    Xander1981
    Last edited by Admin; 10-13-2012 at 08:23 PM. Reason: code tag added

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

    Welcome to ExcelFox !!!

    try something like

    Code:
    If Application.WorksheetFunction.CountIf(Range("T:T"), dDate) Then
        Range("A2").AutoFilter field:=20, Criteria1:=dDate
    End If
    BTW, add code tags while posting code on the forum. This time I added for you.
    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
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    You can modify Admin's code to even give that message you wanted

    Code:
    If Application.WorksheetFunction.CountIf(Range("T:T"), dDate) Then
         Range("A2").AutoFilter field:=20, Criteria1:=dDate
    Else
        MsgBox "No delivery for today!", vbOkOnly
     End If

    https://www.youtube.com/@alanelston2330/featured
    Last edited by DocAElstein; 09-13-2023 at 11:17 AM.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  4. #4
    Thanks both for your very quick responses but this doesn't seem to work. I just changed it to work on column 'U' which displays the date and time so the cell is customs format dd/mm/yyy hh:mm I wonder if this is the problem, but I tried with column 'S' also and still didn't work. I put in today's date to see if it would filter to that row but it didn't and just came up with the msgbox.

    Sorry to be such a beginner.



    https://www.youtube.com/@alanelston2330/featured
    Last edited by DocAElstein; 09-13-2023 at 11:11 AM.

  5. #5
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    If

    dDate and the date you are filtering for (today?) is exactly the same, then it shouldn't be a problem. If the problems still persists, can you attach a sample file.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  6. #6

    still not working :(

    Thanks Excel Fox, yes I see your point. dDate (today's date) and the column I am filtering are the same so I don't know what i'm doing wrong. I think I have missed something silly.

    Code:
    Sub todaysdels()
    
        dDate = Date
        
        If Application.WorksheetFunction.CountIf(Range("U3:U500"), dDate) Then
         Range("A2").AutoFilter field:=20, Criteria1:=dDate
    Else
        MsgBox "No delivery for today!", vbOKOnly
     End If
        
    End Sub
    I tried attaching my file but the size (849 Kb) exceeds the formum limit for xls files.


    https://www.youtube.com/@alanelston2330/featured
    Last edited by DocAElstein; 09-13-2023 at 11:23 AM.

  7. #7

    file attached

    Attached as ZipAttachment 406

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

    Replace the range U3:U500 with T3:T500
    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)

  9. #9
    Thanks Admin, This did filter but showed no rows even though today's date is in row 4. Thanks for your patients.

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

    It works fine here.

    Code:
    Dim rngFilter   As Range
    Dim dDate       As Date
    
    dDate = Date
    
    Set rngFilter = Range("a2:z" & Range("a" & Rows.Count).End(xlUp).Row)
    
    If Application.WorksheetFunction.CountIf(rngFilter.Columns(20), dDate) Then
         rngFilter.AutoFilter field:=20, Criteria1:=dDate
    Else
        MsgBox "No delivery for today!", vbOKOnly
    End If
    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)

Similar Threads

  1. Replies: 2
    Last Post: 09-24-2012, 11:19 PM
  2. Checking Table Exist in Access Database or Not
    By littleiitin in forum Excel and VBA Tips and Tricks
    Replies: 1
    Last Post: 11-16-2011, 04:32 PM
  3. AutoFilter by Multi Color
    By Admin in forum Download Center
    Replies: 0
    Last Post: 09-08-2011, 07:17 AM

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
  •