【发布时间】:2015-02-02 04:46:51
【问题描述】:
下面的代码是我目前所拥有的。但由于某种原因,它说我有一个没有 for 的下一个 soureRow。任何帮助都会很棒。我试图让这个脚本循环遍历第 4 到第 10 页,并且如果该行的背景颜色为黄色或红色,并且第一个没有匹配的值。将该行复制到工作表 1 的底部。
target = "Sheet1"
For allSheets = 4 To 10
lastTargetRow = Sheets(target).Range("A" & Rows.Count).End(xlUp).Row
Sheets(allSheets).Activate
lastCurrentRow = Sheets(allSheets).Range("A" & Rows.Count).End(xlUp).Row
For sourceRow = 2 To lastCurrentRow
If ActiveSheet.Cells(sourceRow, "B").Interior.Color = Yellow Then
For checkRow = 2 To lastTargetRow
If ActiveSheet.Cells(sourceRow, "B").Value <> Sheets(target).Cells(checkRow, "B").Value Then
nRow = Sheets(target).Range("A" & Rows.Count).End(xlUp).Row + 1
For lCol = 1 To 26 'Copy entire row by looping through 6 columns
Sheets(target).Cells(nRow, lCol).Value = Sheets(allSheets).Cells(sourceRow, lCol).Value
Next lCol
End If
Next checkRow
If ActiveSheet.Cells(sourceRow, "B").Interior.Color = Red Then
For checkRow2 = 2 To lastTargetRow
If ActiveSheet.Cells(sourceRow, "B").Value <> Sheets(target).Cells(checkRow, "B").Value Then
nRow = Sheets(target).Range("A" & Rows.Count).End(xlUp).Row + 1
For lCol = 1 To 26 'Copy entire row by looping through 6 columns
Sheets(target).Cells(nRow, lCol).Value = Sheets(allSheets).Cells(sourceRow, lCol).Value
Next lCol
End If
Next checkRow2
End If
Next sourceRow
Next allSheets
【问题讨论】:
-
您在以
If ActiveSheet.Cells(sourceRow, "B").Interior.Color = Yellow Then开头的块上缺少End If
标签: excel vba for-loop copying