Excel

TheSmallman.com

an XL ideas Lab

Dashboards VBA
  • Home
  • Dashboards
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
    • Power Pivot a User Guide
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About
Menu

Excel Dashboards VBA

Street Address
City, State, Zip
Phone Number
an XL ideas Lab

Your Custom Text Here

Excel Dashboards VBA

  • Home
  • Dashboards
  • Excel Tips
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
  • PowerPivot
    • Power Pivot a User Guide
  • Courses
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About

Excel Create or Remove a Table with VBA

June 16, 2016 Marcus Small

There are times in Excel when you may wish to create a table on the fly with the assistance of VBA code. I was in this position recently and needed to this with VBA. A file was uploaded into a sheet and the task was to create a table and then use that table for more data manipulation. The following Excel VBA procedure was what I ended up with.

Option Explicit

Sub
NewTable() 'Excel VBA to create a table if there is none.
Dim ListObj As ListObject
Dim
sTable As String

sTable = "DataTable"

   Set ListObj = ActiveSheet.ListObjects.Add(xlSrcRange, [A1].CurrentRegion, , xlYes)
   ListObj.Name = sTable
'The name for the table

End Sub

To complicate the procedure there was also a possibility that a table had been created by an end user, so I needed to test for the table in which case the code needed to become more complex. The following procedure will remove a table but will keep the formatting of the table so it looks like a table but performs like a normal Excel range.

Sub RemTable() 'Remove table, keep table format.
      Sheet1.ListObjects(1) .Unlist
End Sub

While the following will remove the table and the formatting of the table, so it is just like a worksheet with regular data.

Sub RemTableandFormat() 'Using the Activesheet for variety.
      ActiveSheet.ListObjects("MyData").Unlist
      [A1].CurrentRegion.ClearFormats
End Sub

In both procedures the ListObjects(1) assumes that on sheet1 (the worksheet code name not the sheet name) there is 1 table.

If you are trying to trap the name of a table the following might come in handy. WOrking with the name of the table after it has been created can help with referencing the table at a later point.

Sub TableName() 'Assign the table name to a string
Dim strName As String

strName = Sheet1.ListObjects(1).Name
End Sub

Tags Excel, Table, Clear, Create, remove, VBA
← Water Statistics Infographic in Excel Jump Directly to a Cell with Formula →

Featured Posts

Excel Dashboards: Tracking a Crisis

Excel Dashboards: Tracking a Crisis
April 14, 2020

Recent Posts

Populating an Excel Table from a Range of Cells with VBA

Populating an Excel Table from a Range of Cells with VBA June 12, 2025

Fuzzy Distribution with Randbetween

Fuzzy Distribution with Randbetween May 21, 2025

Add Minimum and Maximum for Chart in Cells

Add Minimum and Maximum for Chart in Cells March 12, 2025

Inflation Over Multiple Years in a Single Cell

Inflation Over Multiple Years in a Single Cell January 10, 2025

Hubspot Dashboard

Hubspot Dashboard October 3, 2024

Monthly Dashboard With Supporting Metrics

Monthly Dashboard With Supporting Metrics September 25, 2024

Excel Show Missing Sheet Tabs

Excel Show Missing Sheet Tabs July 29, 2024

Run Macro Overnight Automatically

Run Macro Overnight Automatically June 24, 2024

Split File into Parts and Save to Directory

Split File into Parts and Save to Directory April 20, 2024

Most Popular Author

Most Popular Author December 14, 2023

 

Follow US:

 
 

MarcusSmall@thesmallman.com

 

TheSmallman.com - Making your small systems hum...
© Copyright 2013-2024 theSmallman.com All Rights Reserved.