复制包含特定文字

时间:2017-07-30 01:20:21

标签: excel vba

如果列B包含某个文本(“ACK-”,我有一个复制整行的代码,但现在我需要将整行直接复制到具有特定文本的行(“ACK-”。这是甚至可能吗?任何帮助都将受到赞赏。

Sub HEA_Filter()
Dim strArray As Variant
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim NoRows As Long
Dim DestNoRows As Long
Dim I As Long
Dim J As Integer
Dim rngCells As Range
Dim rngFind As Range
Dim Found As Boolean

strArray = Array("ack-")

Set wsSource = ActiveSheet

NoRows = wsSource.Range("A65536").End(xlUp).Row
DestNoRows = 1
Set wsDest = Sheets("Real Alarms")

For I = 1 To NoRows

Set rngCells = wsSource.Range("B" & I)
Found = False
For J = 0 To UBound(strArray)
Found = Found Or Not (rngCells.Find(strArray(J)) Is Nothing)
Next J

If Found Then
rngCells.EntireRow.Copy wsDest.Range("A" & DestNoRows)

DestNoRows = DestNoRows + 1
End If
Next I
End Sub

1 个答案:

答案 0 :(得分:1)

要引用“上面的行”,您可以使用Range.Offset方法:

rngCells.Offset(-1).EntireRow.Copy wsDest.Range("A" & DestNoRows)
'       ^^^^^^^^^^^^

但是,请注意,如果范围位于第1行,则会引发运行时错误,因为row 0不存在。您可能需要为其添加检查,例如:

If rngCells.Row > 1 Then rngCells.Offset(-1).EntireRow.Copy ...