简体   繁体   English

如果单元格包含特定文本,请将整行和接下来的两行复制到excel 2013中的下一张工作表

[英]If a cell contains specific text, copy the whole row & the next two rows to next sheet in excel 2013

I am very much new to VBA and i need some help. 我是VBA的新手,我需要一些帮助。

We have 100's of records as shown in the image 如图所示,我们有100条记录 在此处输入图片说明

I would like to copy the row which has "Layer name" in it and the next two rows (Geometry & Feature Count) to the next sheet. 我想将其中具有“图层名称”的行以及接下来的两行(几何和要素计数)复制到下一张工作表。 I tried with the code mentioned here if cell contains specific text, copy whole row + next row Of-course,changed all the '1's to '2'. 我尝试使用此处提到的代码, 如果单元格包含特定文本,则复制整行+下一行 ,将所有的“ 1”更改为“ 2”。 But it didn't work. 但这没有用。 Any sort of help will be appreciated 任何形式的帮助将不胜感激

Try this: 尝试这个:

Sub layer()
j = 1
    For i = 1 To 100
      If Left(Cells(i, 1), 10) = "Layer name" Then
        For k = 0 To 2
          Sheets(2).Cells(j + k, 1) = Cells(i + k, 1)
        Next k
        j = j + 3
      End If
    Next i
End Sub

Try this: 尝试这个:

Sub Test()
For Each Cell In Sheets(1).Range("A:A")

If Left(Cell.Value, 11) = "Layer name:" Then
    matchRow = Cell.Row
    Rows(matchRow & ":" & matchRow + 2).Select
    Selection.Copy

    Sheets(2).Select
    lastRow = ActiveSheet.UsedRange.Rows.Count
    If lastRow > 1 Then lastRow = lastRow + 2
    ActiveSheet.Range("A" & lastRow).Select
    ActiveSheet.Paste
    Sheets(1).Select
End If
Next
End Sub

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

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