Table Rows and Column Header Sequential labelling

Tarkin

New Member
Joined
Feb 12, 2019
Messages
12
Hey Guys

I have the below code which creates a new Column in Table 1 and labels its header as 'B' and creates a new Row in Table 3.

What I want is a sequential labelling of both the New Column header in Table 1 and the first cell of the new Row in Table 3 as 'A', 'B', 'C', etc. whenever a new Column or row is created.

Sub AddNewColumm()
Dim tbl As ListObject
Dim Table As ListObject
Set Table = Sheet1.ListObjects("Table1")
Set tbl = ActiveSheet.ListObjects("Table3")

Table.ListColumns.Add 4
Table.HeaderRowRange(4) = "B"
tbl.ListRows.Add AlwaysInsert:=True
End Sub

Thanks!!
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
According to your macro, the third column of table1 corresponds to the letter "A", the fourth column to the letter "B", then try the following code.

Note: check the name of the objects in blue

Code:
Sub AddNewColumm()
    
    Dim tbl1 As ListObject, tbl3 As ListObject
    Dim numcol As Double, numrow As Double, newcol As Double
    Dim letter As String, newletter As String
    
    Set tbl1 = [COLOR=#0000ff]sheet1[/COLOR].ListObjects("[COLOR=#0000ff]Table1[/COLOR]")
    Set tbl3 = [COLOR=#0000ff]sheet3[/COLOR].ListObjects("[COLOR=#0000ff]Table3[/COLOR]")
    
    'add columns
    numcol = tbl1.ListColumns.Count
    If numcol = 2 Then
        letter = "A"
        newcol = Columns(letter).Column
    Else
        letter = tbl1.ListColumns(numcol)
        newcol = Columns(letter).Column + 1
    End If
    newletter = Replace(Cells(1, newcol).Address(False, False), "1", "")
    tbl1.ListColumns.Add
    tbl1.HeaderRowRange(numcol + 1) = newletter
    
    
    'add rows
    tbl3.ListRows.Add AlwaysInsert:=True
    numrow = tbl3.ListRows.Count
    tbl3.DataBodyRange(numrow, 1).Value = newletter
    
    MsgBox "Column and row created"
End Sub
 

Tarkin

New Member
Joined
Feb 12, 2019
Messages
12
Thanks.

I am getting the following error message:

Run-time error '13': Type mismatch

>with the following code highlighted

newcol = Columns(letter).Column + 1




According to your macro, the third column of table1 corresponds to the letter "A", the fourth column to the letter "B", then try the following code.

Note: check the name of the objects in blue

Code:
Sub AddNewColumm()
    
    Dim tbl1 As ListObject, tbl3 As ListObject
    Dim numcol As Double, numrow As Double, newcol As Double
    Dim letter As String, newletter As String
    
    Set tbl1 = [COLOR=#0000ff]sheet1[/COLOR].ListObjects("[COLOR=#0000ff]Table1[/COLOR]")
    Set tbl3 = [COLOR=#0000ff]sheet3[/COLOR].ListObjects("[COLOR=#0000ff]Table3[/COLOR]")
    
    'add columns
    numcol = tbl1.ListColumns.Count
    If numcol = 2 Then
        letter = "A"
        newcol = Columns(letter).Column
    Else
        letter = tbl1.ListColumns(numcol)
        newcol = Columns(letter).Column + 1
    End If
    newletter = Replace(Cells(1, newcol).Address(False, False), "1", "")
    tbl1.ListColumns.Add
    tbl1.HeaderRowRange(numcol + 1) = newletter
    
    
    'add rows
    tbl3.ListRows.Add AlwaysInsert:=True
    numrow = tbl3.ListRows.Count
    tbl3.DataBodyRange(numrow, 1).Value = newletter
    
    MsgBox "Column and row created"
End Sub
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
Can you put an image of your table? or try to describe the data you have in the table
 

Watch MrExcel Video

Forum statistics

Threads
1,108,492
Messages
5,523,260
Members
409,506
Latest member
reneekeane

This Week's Hot Topics

Top