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!!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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
 
Upvote 0
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
 
Upvote 0
Can you put an image of your table? or try to describe the data you have in the table
 
Upvote 0

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top