从一张纸上复制数据并在同一张工作簿的另一张纸上复印数据

[在这里input图片描述] [1]我想复制行的20-30个单元格的范围,并将其粘贴到另一个表单中。 我做了一个程序,但面对424和1004错误。我试图解决这个错误,但不能,所以我开始应用来自不同网站的不同技巧。 但找不到任何解决办法。 我会感谢你的帮助。 谢谢

Sub CopyRows() Dim LastRow As Integer, i As Integer, erow As Integer Dim copyrange As Range Sheet3.Select 'where is my data lies LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row For i = 4 To LastRow If cells(i, 1).Value = r Then ' condition that should satisfy copyrange = Range(Sheet3.cells(i, 8), sheete3.cells(i, 45)).Select Selection.Copy Worksheets("sheet2").Select erow = ActiveSheet.cells(Rows.Count, 1).End(xlUp).Offset(1, 0) ' erow is last empty row where i wants to copy data ActiveSheet.cells(erow, 3).Select ActiveSheet.Paste ActiveWorkbook.Save End If Next i End Sub 

在这里input图像描述 首先 ,作为一个拇指规则,总是定义和设置您的表格,这将有助于您避免将来出现很多可能的错误。

我不知道你的代码中的r是什么,

If Sheet3.cells(i, 1).Value = "r" Then

如果它是你之前定义的variables(不是你的附加代码的一部分),那么它将起作用。 如果你的意思是字母“r”(一个string),那么你需要修改这个答案中的行:

 If .Cells(i, 1).Value = "r" Then 

除此之外,请使用下面的代码:

 Option Explicit Sub CopyRows() Dim LastRow As Integer, i As Integer, erow As Integer Dim copyrange As Range Dim Sht3 As Worksheet, Sht2 As Worksheet ' always define and set your Sheets >> on the safe side Set Sht3 = ThisWorkbook.Sheets("pending req") Set Sht2 = ThisWorkbook.Sheets("recieve") With Sht3 'find last row with data LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row For i = 4 To LastRow 'If .Cells(i, 1).Value = r Then ' condition that should satisfy (not sure what is r ? ) ' use the line below to check if cell value equals "r" If .Cells(i, 1).Value = "r" Then Set copyrange = .Range(.Cells(i, 8), .Cells(i, 45)) ' erow is last empty row where I want to copy data (assuming you meant Column A) erow = Sht2.Cells(Sht2.Rows.Count, 1).End(xlUp).Row + 1 ' copy a range from one sheet to another without Selecting or activating copyrange.Copy Sht2.Cells(erow, 3) End If Next i End With ThisWorkbook.Save End Sub 

这里清理了一点

 Sub CopyRows() Dim LastRow As Long, i As Long, erow As long Dim copyrange As Range /* where is my data lies LastRow = Sheet3.Range("A" & Rows.Count).End(xlUp).Row For i = 4 To LastRow If Sheet3.cells(i, 1).Value = "r" Then '<--- is ra string here ? put quotes if yes /* condition that should satisfy erow = Sheet2.cells(Rows.Count, 1).End(xlUp).Offset(1, 0) /* erow is last empty row where i wants to copy data Set copyrange = Sheet3.Range(cells(i, 8), cells(i, 45)) copyrange.Copy Destination:= Sheet2.cells(erow, 3) ActiveWorkbook.Save End If Next i End Sub