Convert Range vba code to ListObject vba code

spydey

Active Member
Joined
Sep 19, 2017
Messages
314
Office Version
  1. 2013
Platform
  1. Windows
So I have some code that I have been using for about a year. It works great!!

However, it is for a workbook where all the data is in range format.

I recently re-built the data structure from the ground up in a new workbook, and implemented tables.

It has made things much nicer and easier to reference. Formulas are easier to follow, I need only a minimum of named ranges, etc.

I went from 7 worksheets & 60+ named ranges in the old workbook, to 7 worksheets (each being its own table, so 7 tables) and 1 named range in the new workbook. It is beautiful!

But now I need to re-code my vba to work correctly with the tables.

Here is my previous code. This is just a basic snippet of it:

Code:
Private Sub Separation()


Dim rng As Range
Dim Tracking As Variant
Dim i As Long
Dim wb As Workbook
Dim ws As Worksheet

Set wb = ThisWorkbook
Set rng = wb.Worksheets("Sheet1").Range("Summary")
Set rng = rng.Offset(1).Resize(rng.Rows.Count - 1)

Tracking = rng.Value

For i = LBound(Tracking) To UBound(Tracking)

wb.Worksheets.Copy
    
Set ws = ActiveWorkbook.Worksheets("Sheet1")
                
    With ws
        .AutoFilterMode = False
        .Rows("2:2").AutoFilter
        .Range("Item").AutoFilter Field:=1, Criteria1:="<>" & Tracking(i, 1)
        .UsedRange.Offset(2, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilterMode = False
        .Rows("2:2").AutoFilter
        .Range("Location").AutoFilter Field:=2, Criteria1:="<>" & Tracking(i, 2)
        .UsedRange.Offset(2, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilterMode = False
    End With


Next i

End Sub


***Note that the named range ("Summary") from above is two columns in width.***
***Note that the named range ("Item") from above is the 1st of the two columns from the named range ("Summary").***
***Note that the named range ("Location") from above is the 2nd of the two columns from the named range ("Summary").***

In my new workbook, I no longer have those named ranges as the table object headers match the named ranges.

So I believe that I need to adjust the code that references ranges and named ranges, to correctly reference the Tables and the corresponding column/column header.

Do I or can I simply continue to use references to ranges in my code instead of the table objects?

I am kind of new to re-structuring code from range to listed objects, so I was hoping I could get some pointers, assistance, ideas, etc.

What do you think?

-Spydey
 
Last edited:
Re: Convert Range vba code to ListedObject vba code --- New at this ....

I am not sure why that is happening - - I get the same error

Possibly because a table is an object within the worksheet maybe (a bit like saying Shape.entirerow.delete)?
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Re: Convert Range vba code to ListedObject vba code --- New at this ....

Possibly because a table is an object within the worksheet maybe (a bit like saying Shape.entirerow.delete)?


That is actually quite insightful!!! Makes sense.

Thank you @MARK858 !!

-Spydey
 
Upvote 0
Re: Convert Range vba code to ListedObject vba code --- New at this ....

I have done some further testing
- use a variable to capture the address as a string and use that to define the range to be deleted

Deleting directly FAILS
Code:
    Tbl.DataBodyRange.SpecialCells(xlCellTypeVisible).EntireRow.Delete
Getting the address and putting that inside Range in ONE command FAILS
Code:
    Range(Tbl.DataBodyRange.SpecialCells(xlCellTypeVisible).Address).EntireRow.Delete
But using a variable to capture the address SUCCEEDS
Code:
    Dim addr As String
    addr = Tbl.DataBodyRange.SpecialCells(xlCellTypeVisible).Address
    Range(addr).EntireRow.Delete
 
Last edited:
Upvote 0
Walked away from screen ... and finally "the penny dropped" :laugh:

Doh!

Code:
    On Error Resume Next
    Tbl.DataBodyRange.SpecialCells(xlCellTypeVisible).[COLOR=#ff0000]Rows.[/COLOR]EntireRow.Delete
 
Upvote 0
Not home to test but does the below work?
Code:
DataBodyRange.SpecialCells(xlCellTypeVisible).Rows.Delete
 
Last edited:
Upvote 0
@MARK858

That also works and throws a message asking if user wants to delete entire row
 
Upvote 0
Would need a Display.Alerts False then, thanks for testing.
 
Upvote 0
@Yongle & @MARK858. Thanks for testing out a few snippets of code.

I appreciate the assistance and clarification.

I will have to try the .rows.entirerow.delete.

-Spydey
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top