Excel VBA - Add new table columns with specific header names -
i have simple vba question. want macro add 4 new columns in table object, ("table1"). these named in order, left right:
aht, target aht, transfers, target transfers
the code have below adds columns fine, not sure how name 1 individually. also, please show me how loop section of code. thanks!
sub inserttablecolumn() dim lst listobject dim currentsht worksheet set currentsht = activeworkbook.sheets("sheet1") set lst = activesheet.listobjects("table1") 'below code have looped lst.listcolumns.add lst.listcolumns.add lst.listcolumns.add lst.listcolumns.add end sub
a variant array place store variables in looped sequence.
sub inserttablecolumn() dim lst listobject dim currentsht worksheet dim h long, hdrs variant hdrs = array("aht", "target aht", "transfers", "target transfers") set currentsht = activeworkbook.sheets("sheet1") set lst = activesheet.listobjects("table1") lst 'activesheet.listobjects("table1") h = 0 3 .listcolumns.add .listcolumns(.listcolumns.count).name = hdrs(h) next h end end sub
when creating array of strings in manner, remember variant array's index zero-based default.