所以基本上我在wksht1中有一堆数据要扫描,以选择所有符合if if语句的行(参见下面的代码),然后将它们复制到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
答案 0 :(得分:1)
试试这个:
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
循环,特别是复制到另一个工作表中的下一个空行,你必须记住在循环中检查下一个空行。如果您在开头设置它,它将只接受一个值并一次又一次地覆盖与该值 对应的行 。
现在,以上内容很快被黑客攻击,所以请仔细测试并让我们知道结果。 :)
答案 1 :(得分:1)
范围对象不支持使用Paste
将错误行替换为Cells(LastRowinpastesht, 1).PasteSpecial xlPasteAll
应该清楚这一点。还有其他错误要检查fyi,我不认为Last1
曾经被宣布为任何地方开始。