在表Excel VBA中插入新列 [英] Insert New Column in Table Excel VBA
问题描述
我在正在建立的工作表中遇到一些问题。我想在表中的特定位置插入列,然后设置标题。
I'm running into some issues on a worksheet that I'm building. I want to insert a column at a specific location in a table and then set the header.
我搜索了一段时间,并发现(某些版本)以下代码:
I searched around for a while, and found (some version of) the following code:
ActiveSheet.Columns(2).Insert
这可以插入列,但我不知道如何更改表的标题。
This works to insert the column, but I'm not sure how to change the header of the table after this.
另外,我以前在这里添加了一些表,并将其添加到表的末尾,并给它们命名。
Also, I previously got some advice here on adding columns to the end of a table and naming them.
代码为:
Dim oSh As Worksheet
Set oSh = ActiveSheet
Dim oLc As ListColumn
Set oLc = oSh.ListObjects("PropTable").ListColumns.Add
oLc.Name = "XYZ"
我以各种方式尝试组合这两种方法,但我没有任何运气。有没有办法修改第二个代码块,以便在特定位置插入列,而不是仅添加列?
I tried combining these two methods in various ways, but I'm not having any luck. Is there any way to modify the second block of code so that it inserts a column in a specific location, rather than just adding a column?
谢谢。
-Sean
推荐答案
Dim Table As ListObject
Set Table = Sheet1.ListObjects("Table1")
Table.ListColumns.Add 2
Table.HeaderRowRange(2) = "New header"
这篇关于在表Excel VBA中插入新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!