Forum Discussion

Charles Harris's avatar
Charles Harris
Copper Contributor
Apr 22, 2018

Excel Table Name

Hi

I want to make a selection of say 2 columns  59 rows, open the Table dialog box and in the 'name' box by default, should appear the header of the left hand most column, or a specified cell.   Not Table1 etc.

 

Can a formula be put into the   'Name' box?   to do this.  To save time over a number of tables.

 

Thanks

Charles Harris

3 Replies

  • Haytham Amairah's avatar
    Haytham Amairah
    Silver Contributor

    Charles,

     

    This is can be done, but with some lines of code as follows:

    Sub CreateTable()
        On Error Resume Next
        Dim tableName As String
        tableName = Selection.Cells(1, 1).Value
        If tableName = vbNullString Then Exit Sub
        ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes).name = tableName
        On Error GoTo 0
    End Sub

    This code basically stores the value of the leftmost cell of the selected cells in a variable called tableName, then creates the table, and uses the value stored in this variable to name it.

     

    Please follow this https://www.ablebits.com/office-addins-blog/2013/12/06/add-run-vba-macro-excel/ to figure out how to insert and run this code.

     

    Regards

Resources