Копировать Вставить Очистить
Ссылки
Загрузка рабочей книги (Dropbox)
Код
Sub CopyPasteClear()
'Source
Const cSource As String = "Recieve Tracker" ' Worksheet Name
Const cFirstRsrc As Long = 6 ' First Row Number
Const cClr As String = "B6,D6" ' Clear Cells
Const cRowClr As Long = 8 ' First Clear Row
Const cFinal As String = "G12" ' Final Select Cell Address
' Target
Const cTarget As String = "Data" ' Worksheet Name
' Both
Const cCol1 As Variant = "A" ' First Column Letter/Number
Const cCol2 As Variant = "D" ' Second Column Letter/Number
Dim vntVal As Variant ' Value Array
Dim LastRsrc As Long ' Source Last Row Number
Dim LastRtgt As Long ' Target Last Row Number
' Source Range into Source Array
With ThisWorkbook.Worksheets(cSource)
' Calculate Source Last Row Number of First Column.
LastRsrc = .Columns(cCol1).Find("*", , -4123, , 2, 2).Row
' Prevent copying data above First Row. Rows from First Row to
' one less than First Clear Row will still be copied. To prevent this,
' change cFirstRsrc to cRowClr in the following line only.
If LastRsrc < cFirstRsrc Then Exit Sub
' Copy Source Range into Source Array
vntVal = .Range(.Cells(cFirstRsrc, cCol1), .Cells(LastRsrc, cCol2))
End With
' Source Array into Target Range
With ThisWorkbook.Worksheets(cTarget)
' Check if First Column in Target Worksheet does contain a value.
If Not .Columns(cCol1).Find("*", .Cells(.Rows.Count, _
.Columns.Count), -4123, , 2) Is Nothing Then ' Found.
' Calculate Target Last Row Number of First Column.
LastRtgt = .Columns(cCol1).Find("*", , -4123, , 2, 2).Row
Else ' Not found.
LastRtgt = 0 ' Because 1 will be added in the next line of the code.
End If
' Copy Source Array into Target Range. Note that Target Last Row
' Number has to be inreased by 1 to get the first empty row.
.Cells(LastRtgt + 1, cCol1) _
.Resize(UBound(vntVal), UBound(vntVal, 2)) = vntVal
End With
With ThisWorkbook.Worksheets(cSource)
' Prevent deleting data above First Clear Row.
If LastRsrc < cRowClr Then Exit Sub
' Clear contents of Clear Cells and modified Source Range.
Union(.Range(cClr), .Range(.Cells(cRowClr, cCol1), _
.Cells(LastRsrc, cCol2))).ClearContents
' Activate Source Worksheet if it is not active (not the ActiveSheet).
' The following Select method will produce an error if the program
' was started while a different worksheet than the Source Worksheet
' was active.
If .Parent.ActiveSheet.Name <> .Name Then
.Activate
End If
' Select Final Select Cell.
.Range(cFinal).Select
End With
End Sub