Solved

VBA Excel 2007 deleting visible rows in a table

Posted on 2011-09-04
3
338 Views
Last Modified: 2012-06-27
Hi Experts
I'm trying to filter a table and then delete only the visible rows in an excel table  a snippet of my code below prompts the error

"Delete method of range classs failed"

sheet name: wksRawDataLastWeek
table name: tblRawDataLastWeek


can you assist?


wksRawDataLastWeek.Range("tblRawDataLastWeek").SpecialCells(xlCellTypeVisible).EntireRow.Delete


cheers
0
Comment
Question by:Thrawn3000
  • 2
3 Comments
 
LVL 11

Expert Comment

by:jkpieterse
ID: 36482525
You cannot delete non-contiguous rows in one go. I suggest two options:
1. Sort the table prior to filtering so all rows that need to be removed are together
2. Loop through the visible rows from the bottom up to delete them.
Code for # 2:
Sub DelVisible()
    Dim lCt As Long
    For lCt = Selection.SpecialCells(xlCellTypeVisible).Areas.Count To 1 Step -1 'wksRawDataLastWeek.Range("tblRawDataLastWeek").SpecialCells(xlCellTypeVisible).areas.count
        If Selection.SpecialCells(xlCellTypeVisible).Areas(lCt).Cells(1, 1).Row <> 1 Then
            Selection.SpecialCells(xlCellTypeVisible).Areas(lCt).EntireRow.Delete
        Else
            'Do not delete row 1!
            With Selection.SpecialCells(xlCellTypeVisible).Areas(lCt)
                .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
            End With
        End If
    Next
End Sub

Open in new window

0
 
LVL 11

Accepted Solution

by:
jkpieterse earned 500 total points
ID: 36482528
NB: Replace Selection with:
wksRawDataLastWeek.Range("tblRawDataLastWeek")
0
 

Author Closing Comment

by:Thrawn3000
ID: 37003914
Hi
This has done exactly what I needed it to do, sorry for the late reply wwas on leave.

cheers
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Outlook Free & Paid Tools
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
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…

685 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