Solved

Autofilter error

Posted on 2012-03-21
8
214 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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