Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert all tables in the active worksheet to ranges

I'm making a tool that will cleanup excel documents so that they're in a database-friendly format (for scheduled import into a database).

As part of this tool, I want to convert all tables in the active worksheet to ranges (using VBA). The script below is almost what I want - it converts all tables to ranges for all worksheets in the workbook:

Sub ConvertTablesToRange()
    Dim wks As Worksheet, objList As ListObject
    For Each wks In ActiveWorkbook.Worksheets
        For Each objList In wks.ListObjects
            objList.Unlist
        Next objList
    Next wks
End Sub

Instead of doing this for all worksheets, I just want to do it for the active worksheet. How can I do this?

like image 994
User1974 Avatar asked Feb 10 '17 17:02

User1974


People also ask

How do I convert a table to a range in Excel VBA?

Convert all tables to ranges of active sheet with VBA 1. Activate the sheet you want to convert its tables, then press Alt + F11 keys to enable Microsoft Visual Basic for Applications window. 2. Click Insert > Module to create a new module script, copy and paste below code to the blank module.

What does convert table to range mean?

Excel allows us to convert a table to a range without losing the table style. A range means a regular set of data on the worksheet. This tutorial will walk all levels of Excel users through the easy steps of converting a table to a range while keeping all table style formatting.

How do you convert a cell range to a table using a table style in Excel?

Select any cell in your data set. On the Home tab, in the Styles group, click Format as Table. Pick the desired table style in the gallery. In the Create Table dialog box, make sure the selected range if correct and the My table has headers box is checked, and then click OK.


1 Answers

This should work:

Sub ConvertTablesToRange()
    Dim wks As Worksheet, objList As ListObject
    Set wks = ActiveWorkbook.ActiveSheet
    For Each objList In wks.ListObjects
        objList.Unlist
    Next objList
End Sub
like image 131
Ralph Avatar answered Sep 30 '22 10:09

Ralph