如何根据单元格值在Excel中将行从一个表复制到另一个表

我需要代码复制workbook1中的表的行到基于workbook2E的单元格值的workbook2 。 如果匹配,那么该行需要被复制,并且它所匹配的单元格需要被改变,所以它不会被再次复制

伪代码:

 if workbook1 sheet dispatch range E:E= 13 then copy to workbook WIP sheet1. AND change cell in workbook1 sheet dispatch range E:E to "131". 

我在Dispatch表单上有一个button来运行代码,而不是让它自动运行。

我在这里find的当前代码会删除这一行,但是我需要保留它,因为它已经在另一张纸上制定了时间,并在一天结束时打印出来。

当前代码:

 Sub MoveWaitParts() 'move rows from sheet 1 to sheet 2 if column E has a 13 in it. 'for Move Row into new sheet based on cell valueDim Check As Range lastrow = Worksheets("Dispatch").UsedRange.Rows.Count lastrow2 = Worksheets("wait parts").UsedRange.Rows.Count Dim Response As VbMsgBoxResult Response = MsgBox("Are you sure", vbYesNo Or vbDefaultButton2) If Response = vbYes Then If lastrow2 = 1 Then lastrow2 = 0 Else End If Do While Application.WorksheetFunction.CountIf(Range("E:E"), "13") > 0 Set Check = Range("E1:E4" & lastrow) For Each cell In Check If cell = "13" Then cell.EntireRow.Copy Destination:=Worksheets("wait parts").Range("A" & lastrow2 + 1) cell.EntireRow.Delete lastrow2 = lastrow2 + 1 Else: End If Next Loop End If End Sub 

我不够好的编码,以适应我的需要,有人可以帮忙吗?

你的问题有一些不清楚的元素,但这应该做我相信你正在做的事情。 您的原始代码有一些冗余循环,我发现使用之前计算的LastRow执行一个For循环更容易。 但是,您可以很容易地将该循环转换为“针对范围内的每个单元格”。

 Sub MoveWaitParts() 'Set variables Dim i As Integer Dim DispatchSht As Worksheet Dim WaitSht As Worksheet Set DispatchSht = ThisWorkbook.Sheets("Dispatch") Set WaitSht = ThisWorkbook.Sheets("wait parts") 'Find the last rows in each sheet lastrow = DispatchSht.Cells(DispatchSht.Rows.Count, "E").End(xlUp).Row lastrow2 = WaitSht.Cells(WaitSht.Rows.Count, "A").End(xlUp).Row 'Confirm with user Dim Response As VbMsgBoxResult Response = MsgBox("Are you sure?", vbYesNo Or vbDefaultButton2) If Response = vbYes Then 'Not sure what this is for, but put in by OP 'This will overwrite the first line if there is only one line on the Wait Parts sheet If lastrow2 = 1 Then lastrow2 = 0 End If 'Loop through each row and see if the cell in column E = 13 'If it does, copy the entire row and paste it below the next row on the Wait Parts sheet For i = 1 To lastrow If DispatchSht.Cells(i, "E").Value = "13" Then DispatchSht.Rows(i).EntireRow.Copy Destination:=WaitSht.Rows(lastrow2 + 1) lastrow2 = lastrow2 + 1 '************Unclear question elements********* '//IF THE ADDED 1 IS SOME SORT OF CODE 'DispatchSht.Cells(i, "E").Value = DispatchSht.Cells(i, "E").Value & "1" '//IF THE ADDED 1 IS AN ARITHMETIC FUNCTION OF SOME SORT 'DispatchSht.Cells(i, "E").Value = DispatchSht.Cells(i, "E").Value * 10 + 1 '********************************************** End If Next i End If End Sub