简体   繁体   English

Excel VBA-插入行和插入列宏

[英]Excel VBA - Insert Row & Insert Column Macros

I have a "Task Tracker" workbook that uses columns A:J to display and calculate task information (A:E are for user-entered data, F:J contain formulas that use the information in C:E and perform calculations. F:J are hidden cells in the user view.) The remainder of the columns display a heat map of where the tasks fall on a timeline, and whether or not they are running on time, are behind, or are complete. 我有一本“任务跟踪程序”工作簿,该工作簿使用A:J列显示和计算任务信息(A:E用于用户输入的数据,F:J包含使用C:E中的信息并执行计算的公式。 J是用户视图中的隐藏单元。)其余各列显示热图,显示任务在时间轴上的位置以及它们是否按时运行,是否落后或完成。

There are two buttons for users to use: one to insert a new row, and one to insert a new column. 用户可以使用两个按钮:一个按钮用于插入新行,另一个按钮用于插入新列。 The InsertRow() macro inserts a row into the list, then copies the formulas down from the above row. InsertRow()宏在列表中插入一行,然后从上一行向下复制公式。 The InsertColumn() macro locates the last used column in the worksheet and copies everything over from the column to the left of it. InsertColumn()宏在工作表中找到最后使用的列,并将所有内容复制到该列的左侧。

Originally, I had a macro for InsertRow using Range that copied from Column F (where the formulas start) to Column XFD. 最初,我有一个使用Range的InsertRow宏,该宏从列F(公式开始的地方)复制到列XFD。 However, once I created the InsertColumn macro I realized that I cannot do InsertRow like that because InsertColumn needs to locate the last data-containing column in the worksheet and add a new one to the right...and if InsertRow gets run first, InsertColumn won't work because the value for lastColumn comes back as the index of column XFD. 但是,一旦我创建了InsertColumn宏,我就意识到我不能那样做InsertRow,因为InsertColumn需要在工作表中找到最后一个包含数据的列,并在右侧添加一个新列...如果首先运行InsertRow,则使用InsertColumn将不起作用,因为lastColumn的值作为XFD列的索引返回。

What I am looking for help with: I need to locate the lastColumn value in my InsertRow macro, then use that value as part of the Range when the program executes the Copy/Paste portion of the code. 我正在寻求帮助的内容:我需要在我的InsertRow宏中找到lastColumn值,然后在程序执行代码的复制/粘贴部分时将该值用作Range的一部分。 I think that the problem I'm having has to do with the fact that the code I'm using to find the last column returns the index, and the Range function needs the name of the column. 我认为我遇到的问题与我用来查找最后一列的代码返回索引,而Range函数需要该列的名称有关。

Here is what I have for both macros: 这是我为两个宏所拥有的:

Sub InsertTaskRow()

' InsertTaskRow Macro
'
' This macro inserts a new row below whatever role the user currently has selected, then
' copies the formulas and formatting from above down to the new row

    Dim lastColumn As Long
    Dim currrentRow As Long
    Dim newRow As Long

    lastColumn = ActiveSheet.Range("A1").SpecialCells(xlCellTypeLastCell).Column

    currentRow = ActiveCell.Row
    newRow = currentRow + 1
    ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown

    Range("F" & currentRow & ":" & lastColumn & currentRow).Copy Destination:=Range("F" & newRow & ":" & currentRow & newRow)

End Sub


Sub InsertColumn()

' InsertColumn Macro
'
' This macro copies the formulas and formatting from the last data-containing column
' in the worksheet, and pastes it into the next column to the right.

    Dim lastColumn As Long

    lastColumn = ActiveSheet.Range("A1").SpecialCells(xlCellTypeLastCell).Column

    MsgBox lastColumn

    Columns(lastColumn).Copy Destination:=Columns(lastColumn + 1)

End Sub

You can try changing your lastColumn occurances to the following: 您可以尝试将lastColumn更改为以下内容:

lastColumn = ActiveSheet.Range("A1").End(xlToRight).Column

This will stretch your range to all used cells. 这会将您的范围扩展到所有使用过的单元格。 I tried using the clCellTypeLastCell, but it was pulling further than necessary for no apparent reason; 我尝试使用clCellTypeLastCell,但是由于没有明显的原因,它超出了必要的范围。 even after deleting the entire columns it claimed were applicable. 即使删除它声称的所有列也适用。 Just an FYI, there is no issue with using indexes or column names when utilizing Range() - even interchangibly, they are both fully qualified. 仅供参考,在使用Range()时使用索引或列名没有问题-甚至可以互换,它们都完全合格。

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

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