Solved

Autofilter error

Posted on 2012-03-21
8
220 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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

717 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