Solved

Autofilter error

Posted on 2012-03-21
8
212 Views
Last Modified: 2012-06-27
Hi,

I have the below autofilter code, if i have no data to delete i get the error message

"Object Required"

Is there a line (count line) i can add to get around this?

Thanks
Seamus

With ActiveSheet
   ActiveSheet.Cells.EntireColumn.Hidden = False

    .AutoFilterMode = False
    .Range("A1").AutoFilter Field:=20, Criteria1:="<>Hong Kong"
    End With
 With ActiveSheet
    With .AutoFilter.Range
        On Error Resume Next
        Set RData = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not RData Is Nothing Then
            RData.EntireRow.Delete
        End If
    End With
   
End With
0
Comment
Question by:Seamus2626
  • 4
  • 2
  • 2
8 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
Comment Utility
Which line actually gives the error?
0
 

Author Comment

by:Seamus2626
Comment Utility
If Not RData Is Nothing Then

Thanks
Seamus
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
Comment Utility
Did you declare RData as Range at the top of the code?
0
 
LVL 3

Expert Comment

by:DaFranker
Comment Utility
There's a logical slip in your "If Not RData Is Nothing Then" line. You explicitly set RData earlier to a reference to something (even if the reference points to an object that is empty or does not exist), so RData itself will never be Nothing at that point in the code (barring external intervention/code breaks).

Your code already contains .Rows.Count, which is the property used to know the amoun of rows in the first area of a range. RData.Rows.Count would tell you how many rows there are in the first area of RData.

You could add a check for ".Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Rows.Count" or ".Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Cells.Count" being > 0 to avoid trouble, I believe, though I haven't tested it in those circumstances yet.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Expert Comment

by:Rory Archibald
Comment Utility
>>"There's a logical slip in your "If Not RData Is Nothing Then" line. You explicitly set RData earlier to a reference to something (even if the reference points to an object that is empty or does not exist), so RData itself will never be Nothing at that point in the code (barring external intervention/code breaks)."

That is not correct. If specialcells errors because there are no visible cells, nothing will be assigned and RData will indeed be nothing.
0
 

Author Closing Comment

by:Seamus2626
Comment Utility
Simple as that!
0
 
LVL 3

Expert Comment

by:DaFranker
Comment Utility
Hmm, thanks for the correction, rorya. Could swear I've had that issue before, though, but now that I think about it it was probably related to the use of the New keyword.

Good catch on the variable declaration!
0
 
LVL 85

Expert Comment

by:Rory Archibald
Comment Utility
Yeah - one very good reason to never use the 'Dim ... As New ...' syntax is that you can't test for Nothing.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvieā€¦
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calculā€¦

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now