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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21712678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:52:13  来源:igfitidea点击:

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

excelvbaexcel-vba

提问by amfrancis

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. 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.

我正在尝试构建的宏定位最后一列,并将整个列 lastColumn 复制到列 newColumn 中。但是,我在网上找到并尝试适应的所有内容要么给我一个对象错误,要么什么都不做。请帮我弄清楚如何做到这一点。

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

采纳答案by Alex P

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