2013-06-27 41 views
3

因此,我正在處理這個宏,該宏會根據表中的其他列自動將列添加到表中。所以這裏的功能:在Excel中通過宏將列添加到表中時設置列標題

我有這個表中的「CY 2010」 - 「CY 2020」的列數。這些年將不可避免地發生變化。然後我想爲每個「CY」列添加一列到表中。此外,我想這些列的標題來一年匹配,但說「內容量同比」

目前

,我有以下的代碼,適當添加列:

Sub AddContentValueColumns() 
' 
' AddContentValueColumn Macro 
' Adds yearly vehicle content value columns to Propulsion data table. 
' 

' 
    last = [A1].Value    ' cell contains number of "CY" columns in sheet 

' Debug.Print (last) 

    For Count = 1 To last 
     Dim oSh As Worksheet 
     Set oSh = ActiveSheet 
     oSh.ListObjects("PropTable").ListColumns.Add 
    Next Count 

End Sub 

這個工程添加列,但它只是將列添加到標記爲「CY 2021」的表末尾等。

那麼,如何修改上面的代碼以命名列?

謝謝。

-Sean

回答

4

下面是如何更改列名的例子:

last = [A1].Value    ' cell contains number of "CY" columns in sheet 
For Count = 1 To last 
    Dim oSh As Worksheet 
    Set oSh = ActiveSheet 
    Dim oLc As ListColumn 
    Set oLc = oSh.ListObjects("PropTable").ListColumns.Add 
    oLc.Name = "COLUMN NAME" & last 
Next Count 
+0

這完美地工作。謝謝你的幫助! – detroitwilly

+0

+1,我從來沒有意識到ListColumns有名字!這將簡化一些代碼。 –

相關問題