Copy entire table column with formulas

时间:2019-04-17 00:06:43

标签: excel vba

I have an Excel Table and I am scrolling its columns to add new columns in the right position (sorted alphabetically) I would then like to copy all formulas from the previous (or following) column in the table. I have tried:

Columns(x).Copy
ActiveSheet.Paste

But I can't get it to work. Basically I have a userForm used to input the name of the new column (Cost Centre) and then scroll some columns to insert it in the right position, as follows:

Dim CDCName As String
CDCName = txtCDC.Text

Dim Tbl As ListObject
Dim Col As ListColumn
Dim Position As Integer

For Each Col In Tbl.ListColumns
    If Col.Index > 5 And Col.Index < Tbl.ListColumns.Count - 3 Then
        If Col.Range(1, 0).Value > CDCName Then
            Position = Col.Index - 1
            Set NewCol = Tbl.ListColumns.Add(Position)
            Col.Range(1, -1).Value = CDCName
            Unload Me
            MsgBox "Cost Centre " & CDCName & " added to table"
            Exit For
        End If                
    End If
Next

1 个答案:

答案 0 :(得分:0)

I think I managed to solve it using:

Tbl.ListColumns(Position - 1).Range.Copy Tbl.ListColumns(Position).Range

after adding the column with Set NewCol = Tbl.ListColumns.Add(Position) like in my first example

相关问题