问题描述
我想循环浏览工作表,如果满足内部条件,则将其复制到我的“ Controle”工作表中。
我的代码没有循环。
我在“控件”工作表中有一个列表,其中包含所有工作表中的所有彩色单元格。该代码必须跳过名称为“ Controle”的工作表,因为在这里我将单元格复制到其中。
Sub dubbelewaarden()
Dim cell As Range
Dim sht As Worksheet
For Each sht In Worksheets
If Not sht.Name <> "Controle" Then
For Each cell In Range("A2",Range("A2").End(xlDown))
If cell.Interior.ColorIndex = 3 Then
cell.EntireRow.copy Destination:=Sheets("Controle").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next cell
End If
Next sht
End Sub
```
解决方法
这只是将loop变量调整到工作表的一种情况,否则将假定为活动工作表。在线有很多参考。
Sub Dubbelewaarden()
Dim cell As Range
Dim sht As Worksheet
For Each sht In Worksheets
If sht.Name <> "Controle" Then
'tie reference to sheet in loop
For Each cell In sht.Range("A2",sht.Range("A" & Rows.Count).End(xlUp))
If cell.Interior.ColorIndex = 3 Then
cell.EntireRow.Copy Destination:=Sheets("Controle").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next cell
End If
Next sht
End Sub