Копировать строки
Sub RowsCopy()
Const cSource As Variant = "Sheet1" ' Source Worksheet Name/Index
Const cTarget As Variant = "Sheet2" ' Target Worksheet Name/Index
Const cRange As String = "A7" ' Source Cell Range Address
Const cCrit As Long = 4 ' Criteria
Const cRowsA As Long = 5 ' Source Rows Above
Const cRowsB As Long = 20 ' Source Rows Below
Dim ws As Worksheet ' Target Worksheet
Dim FER As Long ' Target First Empty Row
' In Source Cell Range
With ThisWorkbook.Worksheets(cSource).Range(cRange)
' Create a reference to Target Worksheet.
Set ws = .Parent.Parent.Worksheets(cTarget)
' Calculate Target First Empty Row using column 1 (A).
FER = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
' Check if value in Source Cell Range meets (is equal to) Criteria.
If .Value = cCrit Then
' Calculate Target Range Above.
' Calculate Source Range Above.
' Copy values of Source Range Above to Target Range Above.
ws.Rows(FER).Resize(cRowsA).Value _
= .Worksheet.Rows(.Row - cRowsA).Resize(cRowsA).Value
' Calculate new Target First Empty Row by adding Source Rows Above.
FER = FER + cRowsA
' Calculate Target Range Below.
' Calculate Source Range Below.
' Copy values of Source Range Below to Target Range Below.
ws.Rows(FER).Resize(cRowsB).Value _
= .Worksheet.Rows(.Row + 1).Resize(cRowsB).Value
End If
End With
End Sub
Лист1
Лист2
Копирование и удаление строк
Sub RowsCopyDelete()
Const cSource As Variant = "Sheet1" ' Source Worksheet Name/Index
Const cTarget As Variant = "Sheet2" ' Target Worksheet Name/Index
Const cRange As String = "A7" ' Source Cell Range Address
Const cCrit As Long = 4 ' Criteria
Const cRowsA As Long = 5 ' Source Rows Above
Const cRowsB As Long = 20 ' Source Rows Below
Dim ws As Worksheet ' Target Worksheet
Dim rng As Range ' Delete Range
Dim FER As Long ' Target First Empty Row
' In Source Cell Range
With ThisWorkbook.Worksheets(cSource).Range(cRange)
' Create a reference to Target Worksheet.
Set ws = .Parent.Parent.Worksheets(cTarget)
' Calculate Target First Empty Row using column 1 (A).
FER = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
' Check if value in Source Cell Range meets (is equal to) Criteria.
If .Value = cCrit Then
' Calculate Target Range Above.
' Calculate Source Range Above.
' Create a reference to Target Range Above (Delete Range).
Set rng = .Worksheet.Rows(.Row - cRowsA).Resize(cRowsA)
' Copy values of Source Range Above to Target Range.
ws.Rows(FER).Resize(cRowsA).Value = rng.Value
' Calculate new Target First Empty Row by adding Source Rows Above.
FER = FER + cRowsA
' Calculate Target Range Below.
' Calculate Source Range Below.
' Add Target Range Below to Delete Range.
Set rng = Union(rng, .Worksheet.Rows(.Row + 1).Resize(cRowsB))
' Copy values of Source Range Below to Target Range Below.
ws.Rows(FER).Resize(cRowsB).Value _
= .Worksheet.Rows(.Row + 1).Resize(cRowsB).Value
' Delete Source Rows
rng.Rows.Delete ' .Hidden = True, .ClearContents, .Clear
End If
End With
End Sub