简体   繁体   English

Excel宏查找使用的最后一列,插入新列并从上一列复制公式

[英]Excel Macro find last column used, insert new column and copy formulas from previous

I am trying to create an "Insert Column" macro in Excel. 我正在尝试在Excel中创建“插入列”宏。

The workbook uses formulas and conditional formatting to display progress along a timeline. 该工作簿使用公式和条件格式来显示时间轴上的进度。 I need to provide users with a way to add additional columns to the timeline. 我需要为用户提供一种向时间轴添加其他列的方法。

The macro I am trying to build locates the last column and copies the entirety of column lastColumn into column newColumn. 我正在尝试建立的宏将查找最后一列,并将整个列lastColumn复制到列newColumn中。 However, everything that I find online and try to adapt either gives me an object error or doesn't do anything. 但是,我在网上找到的所有尝试修改的东西都给了我一个对象错误,或者什么也不做。 Please help me figure out how to do this. 请帮我弄清楚该怎么做。

Here's my code so far. 到目前为止,这是我的代码。

Sub InsertColumn()

    Dim lastColumn As Long
    Dim newColumn As Long

    With ActiveSheet
        lastColumn = .Range("A1").SpecialCells(xlCellTypeLastCell).column
    End With

    newColumn = lastColumn + 1

    Selection.AutoFill Destination:=Columns(lastColumn & ":" & newColumn), Type:=xlFillDefault

End Sub

If you are simply trying to copy the one column to another then this will work: 如果您只是尝试将一列复制到另一列,则可以使用:

Sub InsertColumn()
    Dim lastColumn As Long
    lastColumn = ActiveSheet.Range("A1").SpecialCells(xlCellTypeLastCell).Column
    Columns(lastColumn).Copy Destination:=Columns(lastColumn + 1)
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM