2

I have a simple vba question. I want a macro that will add exactly 4 new columns in my table object, ("Table1"). I would also like these to be named in order, from left to right:

AHT, Target AHT, Transfers, Target Transfers

The code I have below adds the columns just fine, but I am not sure how to name one individually. Also, could someone please show me how to loop that section of code. Thanks!

Sub insertTableColumn()
Dim lst As ListObject
Dim currentSht As Worksheet

Set currentSht = ActiveWorkbook.Sheets("Sheet1")
Set lst = ActiveSheet.ListObjects("Table1")

'below is the code that I would like to have looped
    lst.ListColumns.Add
    lst.ListColumns.Add
    lst.ListColumns.Add
    lst.ListColumns.Add

End Sub

2 Answers 2

2

A variant array is a good place to store the variables in a looped sequence.

Sub insertTableColumn()
    Dim lst As ListObject
    Dim currentSht As Worksheet
    Dim h As Long, hdrs As Variant

    hdrs = Array("AHT", "Target AHT", "Transfers", "Target Transfers")

    Set currentSht = ActiveWorkbook.Sheets("Sheet1")
    Set lst = ActiveSheet.ListObjects("Table1")

    With lst 'ActiveSheet.ListObjects("Table1")
        For h = 0 To 3
            .ListColumns.Add
            .ListColumns(.ListColumns.Count).Name = hdrs(h)
        Next h
    End With

End Sub

When creating an array of strings in this manner, remember that the variant array's index is zero-based by default.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the bit about the zero-based index. I certainly would have questioned the 0.
I know this is old, but you can avoid the whole "0-based" issue if you change the hard coded: For h = 0 To 3 to the following: For h = Lbound(hdrs) to Ubound(hdrs) . This has the advantage of only having to maintain what goes into your array.
2

The following code creates an array of the column names you want to add, then loops as many times as there are headers to add and sets the name at the same time.

Sub insertTableColumn()
Dim lst As ListObject
Dim currentSht As Worksheet

Set currentSht = ActiveWorkbook.Sheets("Sheet1")
Set lst = ActiveSheet.ListObjects("Table1")
ColumnNames = Array("AHT", "Target AHT", "Transfers", "Target Transfers")

' below is the code that I would like to have looped
For iLoop = 0 to UBound(ColumnNames)
    Set oLC = lst.ListColumns.Add
    oLC.Name = ColumnNames(iLoop)
Next

End Sub

2 Comments

Thank you very much for that introduction to the UBound Function. I had not run into that before. Just a quick general question - If I wanted to declare your "ColumnNames" variable, how would I do so? I tried to declare it as a string and my code generated an error. What is best practice for declaring variables you are going to use as an array? Thanks again!
You can just use Dim ColumnNames As Variant (I should have added that line to be honest), but if you don't specify what a variable type is, VBA will automatically define it as Variant, which will accept pretty much any type really, so you could get away with simply Dim ColumnNames :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.