VBA复制并粘贴到另一个工作表

我是编程新手,需要一些帮助:我需要一个代码,当它读入单元格(x,3)=“wall”时,对于每一个下一行,直到它“击中”另一个元素,直到单元格(x + 1,3)<>“”,如果它们满足特定条件,则将该行的单元格A:E的值复制到另一个表格。 代码将以某种方式开始:

If Cells(x, 3) = "wall" Then Do Until Cells(x + 1, 2) <> "" If Cells(x + 1, 4) <> "m2" Then ...... x=x+1 Loop 

我想在中间的代码的一部分帮助。

尝试下面的代码。 确保它正在为您的条件查看正确的单元格。

 Option Explicit Sub copyCells() 'Some variables to keep track of where we are on the sheets Dim x As Integer Dim lastRow As Integer Dim i As Integer Sheets("Sheet1").Select Range("A1").Select 'I used 18 rows in my test set. You'll want to change this to fit your data. lastRow = 18 i = 1 For x = 1 To lastRow 'Check for the first condition If Cells(x, 3) = "wall" Then 'Move to the next row x = x + 1 'Check that this is a row we want to copy 'and we haven't reached the end of our data Do While Cells(x, 2) = "" And x < lastRow 'Check the second condition If Cells(x, 4) <> "m2" Then 'Copy and paste to the second sheet Range("A" & x & ":E" & x).Copy Sheets("Sheet2").Select Range("A" & i).Select ActiveSheet.Paste 'Increment i to keep track of where we are on the second sheet i = i + 1 End If 'Go back to checking the first sheet x = x + 1 Sheets("Sheet1").Select Range("A" & x).Select Loop End If Next x Application.CutCopyMode = False End Sub