我想将具有公式的最后一行从工作表复制到下一行,然后将我刚刚复制的行注释掉。例如,我在A到W列中有公式,我当前的最后一行是10,我想将第10行复制到第11行然后将第10行赋值。试图获取vba代码以自动化进程。任何帮助表示赞赏。
答案 0 :(得分:0)
这应该让你开始朝着正确的方向前进:
Sub appendToEnd()
Dim myLastCell As Range
Set myLastCell = LastCell(Worksheets("Sheet1").Range("A:W"))
Dim lastCellCoords As String: lastCellCoords = "A" & myLastCell.Row & ":W" & myLastCell.Row
Dim firstEmptyRow As Integer: firstEmptyRow = myLastCell.Row + 1
Dim firstEmptyCoords As String: firstEmptyCoords = "A" & firstEmptyRow & ":W" & firstEmptyRow
If Not myLastCell Is Nothing Then
' Now Copy the range:
Worksheets("Sheet1").Range(lastCellCoords).Copy
' And paste to first empty row
Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Else
MsgBox ("There is no data in specified range")
End If
End Sub
Function LastCell(r As Range) As Range
'
' Note "&" denotes a long value; "%" denotes an integer value
Dim LastRow&, lastCol%
On Error Resume Next
With r
' Find the last real row
LastRow& = .Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
' Find the last real column
lastCol% = .Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
End With
' Finally, initialize a Range object variable for
' the last populated row.
Set LastCell = r.Cells(LastRow&, lastCol%)
End Function