Delete rows using AutoFilter and SpecialCells properties

  • Hello forum,

    The line that is meant to apply the autofilter to the range A5 to Q & LastRow is not executing as expected.

    The error message I get is "can't be applied to the selected range. Select a single cell in a range and try again."

    How can I correct this?


    Happy New Year!

  • You'll get an error if a different range is already set with a filter. Use:

    Code
    ws.AutoFilterMode = False


    rather than ShowAllData

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Hi Rory, I tried your suggestion and received the same error message. The macro is being used on an excel file that is system generated and opened from email (Outlook). As far as I know there are no ranges set before I get the file and there are no other filters in use, that I can see. I'm still a novice with using VBA so I apologize if I am missing any of the basics and I probably don't have a thorough understanding of Range and Range Properties. To my thinking it's strange that the error message is requesting that a single cell needs to be selected in a range and to try again. Isn't the whole idea of using a filter based on multiple cells?

  • Is the macro in the workbook you're trying to run it on? I not, you should be referring to ActiveWorkbook, not ThisWorkbook.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Excellent that worked! But before I read your post I was playing with this and tried to set the Worksheet differently using the code below.

    The error I encountered was "object required". Isn't a Worksheet an object?


    Code
    Dim wsActiveSheet As Worksheet
    Set wsActiveSheet = Application.ActiveSheet


    Thanks and Happy New Year!

  • Yes, it is. There is nothing wrong with that code that I can see. It certainly shouldn't produce the error you describe.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!