如何使用VBA插入新列并用其他工作表列值填充?

时间:2018-12-07 15:53:55

标签: excel vba

我正在尝试创建一个新列,并且只能创建一个新列而不能插入值。我的目标是将Sheet1的C列插入Sheet2的B列中以获得Sheet3的结果。

Sheet1:

enter image description here

Sheet2:

enter image description here

Sheet3:

enter image description here

这是我到目前为止尝试过的:

Sub AddColumn(length As Integer)
    Dim cell As String
    Dim form As String
    Worksheets("Sheet2").Activate
    Dim i, lastColumnNbr As Long
    Dim lastColumnStr As String
    lastColumnNbr = Worksheets("Sheet2").UsedRange.Columns.Count + 1
    lastColumnStr = ColLetter(lastColumnNbr)
    ColumnName = Split(base, " ")
    Range(lastColumnStr & 1) = ColumnName(1)
    Application.CutCopyMode = False
    For i = 2 To length
        cell = lastColumnStr & i
        form = "=TEXT("B"&i,"m/d/yyyy")"
        Range(cell).Formula = form
    Next
    Columns("A:A").Select
    Application.CutCopyMode = False
    Selection.Copy
    Columns(LastColumnNbr).Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
End Sub

提前谢谢

0 个答案:

没有答案