Solved

Autofilter error

Posted on 2012-03-21
8
213 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
ID: 37747795
Which line actually gives the error?
0
 

Author Comment

by:Seamus2626
ID: 37747839
If Not RData Is Nothing Then

Thanks
Seamus
0
 
LVL 85

Accepted Solution

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

Expert Comment

by:DaFranker
ID: 37747918
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 37747948
>>"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
ID: 37747986
Simple as that!
0
 
LVL 3

Expert Comment

by:DaFranker
ID: 37748073
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
ID: 37748090
Yeah - one very good reason to never use the 'Dim ... As New ...' syntax is that you can't test for Nothing.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create a Dynamic Dropdown List in Excel excluding Blanks 5 35
time format showing wrong 12 49
Problem to With line 4 37
Help with Adding text from a form to a worksheet 5 35
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

932 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

8 Experts available now in Live!

Get 1:1 Help Now