如何通过我的数据扫描我的代码并复制某些选定的数据行并将其粘贴到其他表单中?

所以基本上我有wksht1中的一堆数据,我想扫描以select所有遵守某个if then语句(见下面的代码)的行,然后将它们复制到pastesht。 我有我的代码到目前为止,但它给了我一个错误,在第20行,我不知道我需要修复。 请提前帮忙,谢谢!

1 Sub Try() 2 Dim wksht1 As Worksheet, pastesht As Worksheet 3 Dim LastRowinpastesht As Long 4 5 With ThisWorkbook 6 Set wksht1 = Sheets("AAA") 7 Set pastesht = Sheets("PPP") 8 LastRowinpastesht = pastesht.Cells(Rows.Count, 1).End(xlUp).Row + 1 9 End With 10 11 With wksht1 12 Last1 = Cells(Rows.Count, "B").End(xlUp).Row 13 For p = Last1 To 1 Step -1 14 If (Cells(p, "F").Text) = "SSSS" And (Cells(p, "U").Value) <= 5 And (Cells(p, "U").Interior.ColorIndex) = xlNone Then 15 Cells(p, "A").EntireRow.Select 16 Selection.Copy 17 End If 18 19 With pastesht 20 Cells(LastRowinpastesht, 1).Paste 21 Application.CutCopyMode = False 22 End With 23 Next p 24 End With 25 End Sub 

尝试这个:

 Sub TryMod() Dim WS1 As Worksheet, PasteSht As Worksheet Dim PShtNRow As Long, WS1LRow As Long With ThisWorkbook Set WS1 = .Sheets("AAA") Set PasteSht = .Sheets("PPP") End With With WS1 WS1LRow = .Cells(Rows.Count, "B").End(xlUp).Row For Iter = WS1LRow to 1 Step -1 PShtNRow = PasteSht.Cells(PasteSht.Rows.Count, 1).End(xlUp).Row + 1 If .Cells(Iter, "F").Value = "SSSS" And .Cells(Iter, "U").Value <= 5 And .Cells(Iter, "U").Interior.ColorIndex = xlNone Then .Rows(Iter).Copy PasteSht.Rows(PShtNRow) End If Next Iter End With Application.CutCopyMode = False End Sub 

主要的问题是当你使用With时你必须限定你的属性和方法。 你不能With WS1然后使用Cells 。 它必须是.Cells 。 否则,Excel将不会知道您所指的是哪个工作表的Cells ,以及可能发生的其他可能的错误。

接下来是,关于For循环,尤其是复制到另一个表中的下一个空行,你必须记住把循环内下一个空行的检查。 如果你在开始时设置它,它将只取一个值, 并且一次又一次地覆盖对应于该值的行。

现在,上面的内容真的很快就被破解了,所以请试试,让我们知道结果。 🙂

范围对象不支持使用Paste

Cells(LastRowinpastesht, 1).PasteSpecial xlPasteAllreplace错误的行Cells(LastRowinpastesht, 1).PasteSpecial xlPasteAll

那应该清除那个。 还有其他的错误检查只是fyi,我不认为Last1曾经任何地方开始。