[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Autofilter error

Posted on 2012-03-21
8
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 37747900
Did you declare RData as Range at the top of the code?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Frank White
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
 
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:Frank White
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

650 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