Managing the data with Tables is very easy in Excel. We can do lot more things if your data is in the form of tables. In this section we will show some of the Tables operations using Excel VBA.

Create Tables in Excel VBA:

Sometimes you may want to create Tables in Excel VBA. Please find the following code to Create Tables in Excel VBA.

Create Tables in Excel VBA – Solution(s):

You can use ListObjects.Add method of sheet to create tables in excel VBA. We can create table in the following way. The following code creates a table “myTable1” and referring to sheet1 of a range(“$A$1:$D$10”) .

Code:
'Naming a range
Sub sbCreatTable()

    'Create Table in Excel VBA
    Sheet1.ListObjects.Add(xlSrcRange, Range("A1:D10"), , xlYes).Name = "myTable1"

End Sub
Output:

Create Tables in Excel VBA

Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Double click on ThisWorkbook from Project Explorer
  4. Copy the above code and Paste in the code window
  5. Press F5
  6. GoTo Sheet1 and Select Range A1 to D10
  7. You should see the above output in Sheet1
Reset a Table back to Normal Range

If you want to Reset the table back to original range, you can use Unlist property of table object. Following code will show you how to remove table formats and reset to normal range.

Sub sbReset_Table_BackTo_Range()
    
    'Reset Table Back to Original Range
    On Error Resume Next  'If there are no Table ignore the below Statement
    Sheet1.ListObjects("myTable1").Unlist
    
End Sub
Example File

Download the example file and Explore it.
Analysistabs – Create Tables in Excel VBA

Sorting Tables in Excel VBA:

Examples for Sorting Table in Excel VBA with using sort method of ListObjects. You can learn how to sort table with examples.

Sorting Table in Excel VBA – Solution(s):

You can use sort method of ListObjects for sorting table in Excel VBA. We can do sort data in the following way.

Code:
Sub sbSortTable()

     'Naming a range
     Sheet1.Sheets("Sheet1").ListObjects("myTable1").Sort.SortFields.Clear
     Sheet1.Sheets("Sheet1").ListObjects("myTable1").Sort.SortFields.Add Key:=Range("myTable1
[[#All],[EmpName]]"), SortOn:=sortonvalues, Order:=xlAscending, DataOption:=xlSortNormal Range("myTable1[#All]").Select With Sheet1.Worksheets("Sheet1").ListObjects("myTable1").Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub
Output:
Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Double click on ThisWorkbook from Project Explorer
  4. Copy the above code and Paste in the code window
  5. Press F5
  6. GoTo Sheet1 and Select Range A1 to D10
  7. You should see the above output in Sheet1

Filtering Tables in Excel VBA

Sometimes you may want to Filter Tables in Excel VBA. Please find the following code for Filtering Tables in Excel VBA.

Filtering Tables in Excel VBA – Solution(s):

You can use ListObjects(“TableName”).Range.AutoFilter method for Filtering tables in excel VBA. We can filter table in the following way. The following code filters a table “myTable1” and referring to sheet1 of a range(“$A$1:$D$10”).In this Case I am applying filter for second column and looking for description “DDD” in a table.

Code:
'Filtering a table
Sub sbFilterTable()

     ActiveWorkbook.Sheets("Sheet1").ListObjects("myTable1").Range.AutoFilter field:=2, Criteria1:="DDD" 'matched with 4 in column c2 records will be shown

End Sub
Output:

Filtering Tables in Excel VBA

Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Double click on ThisWorkbook from Project Explorer
  4. Copy the above code and Paste in the code window
  5. Press F5 to execute Macro
  6. GoTo Sheet1 and Select Range A1 to D10
  7. You should see the above output in Sheet1
Example File

Download the example file and Explore it.
Analysistabs – Filtering Tables in Excel VBA

Clear or Toggle Table Filters in Excel VBA:

Examples for Clear Toggle Table Filters in Excel VBA with using FilterMode Property and AutoFilter method. You can learn how to Clear Toggle Table Filters in Excel VBA with following example.

Clear Toggle Table Filters in Excel VBA – Solution(s):

You can Clear Toggle Table Filters in Excel VBA with using FilterMode Property and AutoFilter method in Excel VBA. We can do Clear table filter in the following way.

Code:
'Clear Table Filter
Sub sbClearFilter()

    'Check Filter is Exists or Not
    If ActiveWorkbook.Sheets("Sheet1").FilterMode = True Then
    
        ActiveWorkbook.Sheets("Sheet1").ListObjects("myTable1").Range.AutoFilter
        
    End If

End Sub
Output:

Clear Toggle Table Filters in Excel VBA

Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Double click on ThisWorkbook from Project Explorer
  4. Copy the above code and Paste in the code window
  5. Press F5 to execute Macro
  6. GoTo Sheet1 and check the Table Data from A1 to D10
  7. You should see the above output in Sheet1
Example File

Download the example file and Explore it.
Analysistabs – Clear Tables in Excel VBA

120+ Professional Project Management Templates!
Save Up to 85% LIMITED TIME OFFER

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Browse All Templates
Excel VBA Project Management Templates

All-in-One Pack
120+ Project Management
Premium Templates
View Details

Essential Pack
50+ Project Management
Premium Templates
View Details
50+ Excel
Project Management
Templates Pack
View Details
50+ PowerPoint
Project Management
Templates Pack
View Details
25+ MS Word
Project Management
Templates Pack
View Details
Ultimate Project Management Template
View Details
Ultimate Resource Management Template
View Details
Project Portfolio Management Templates
View Details
By Last Updated: June 17, 2022Categories: Excel VBATags:

Share This Story, Choose Your Platform!

13 Comments

  1. Bruce January 13, 2014 at 2:20 AM

    Excellent! Now the couple pieces missing:

    1) Copying the filtered results to a new sheet Sheet02 (and whether or not the copied area is a range, table or neither at that time)
    2)Applying the same process to a new range of data and adding that filtered section to the previously established data on the new sheet Sheet02
    3) Sorting the new sheet data every time data is added and removing duplicates based upon one field being used as a key.

    I am sure I will run into all this, but you folks were off to such a great start of the information being clear and collected in one place, I wanted to place the suggestion, provided you ever have the time!

  2. PNRao January 13, 2014 at 9:14 PM

    Hi Bruce,

    Thanks for your suggestion, I will add these in couple of weeks.

    Thanks-PNRao!

  3. Nurit May 27, 2014 at 10:11 PM

    How do you create a table when the range is dynamic. I want to automate the process of refreshing and opening pivot tables. I need to:
    1) import data from a software package (this will be done manually)
    2) open the file created in #1 and create and name table. The range of data is unknown.
    3) open the pivot table which will refresh automatically using the named table created in #2.

    I got this far – how do I change the defined range to a dynamic range and do I need the last line?

    Sub opengetfile()
    Dim strFileName As String
    Dim rgData As Range
    strFileName = Application.GetOpenFilename
    If strFileName = “False” Then Exit Sub
    MsgBox strFileName
    Workbooks.Open (strFileName)

    ActiveSheet.ListObjects.Add(xlSrcRange, Range(“$A$1:$AX$224”), , xlYes).Name = “Loans”
    Range(“Loans[#All]”).Select

    End Sub

  4. PNRao May 28, 2014 at 11:45 PM

    Hi Nurit,

    Welcome to ANALYSITABS!

    You can create the dynamic range by finding the last row:
    (examples shown here: http://analysistabs.com/excel-vba/finding-last-used-row-with-data-worksheet/)

    Now your code will be:

    lastRow=200 ‘ This is Dynamic in your case, please use the example in the above link to find last row
    ‘Now Your range will be:
    ActiveSheet.ListObjects.Add(xlSrcRange, Range(“$A$1:$AX$″ &lastRow), , xlYes).Name = “Loans”

    ‘Now create the pivot table
    (examples shown here: http://analysistabs.com/excel-vba/pivot-tables-examples/ )

    Hope this helps!
    Thanks-PNRao!

  5. Gejza Horvath June 16, 2014 at 1:42 AM

    If all the data on this sheet will be inside a listobject, you can use ActiveSheet.UsedRange property. Instead the Range() object.

  6. JonasTiger December 29, 2014 at 3:44 AM

    Hi
    Filtering Tables in Excel VBA…
    I need to filter my table everytime data changes.
    In your example, you filter “DDD”.
    In mine, I want filter all options except zero/blank cells of a range, i.e., its like excluding “DDD” and leave remaining data.
    please help me changing code for that.
    Thank you

  7. Amy January 30, 2015 at 5:27 AM

    This has been very helpful! I needed to create a macro to turn a dynamic range into a table, sort by a specific column, & filter the table based on that same column name. This page had all the information I needed.
    I look forward to checking out the other areas of your site.

    Thanks for sharing.

  8. yonsebastian June 1, 2015 at 5:52 AM

    working for year with VBA now 2013 comes with tables which is awesome!

  9. PNRao June 1, 2015 at 12:20 PM

    Yes, Tables or ListObjects in Excel are very handy to deal with the data. We can perform many operations and its fast.

    Thanks-PNRao!

  10. Agni June 10, 2016 at 12:31 PM

    Hi,
    How to create a table without default auto filter and header.

  11. prema April 2, 2018 at 11:49 AM

    If ActiveWorkbook.Sheets(“Sheet1”).FilterMode = True Then

    is not working for a listobject

  12. a February 17, 2020 at 9:28 AM

    [code]
    Sub Next ()
    If G3=1 Then
    Next i
    Me.ListBox1.Column = Tbl
    End If
    End Sub

  13. a February 17, 2020 at 9:29 AM

    [code]
    Sub Next ()
    If G3=1 Then
    Next i
    Me.ListBox1.Column = Tbl
    End If
    End Sub
    [/code]

Leave A Comment