迭代太多:仅在满足所有条件后才需要语法来突出显示单元格行
Too many iterations: syntax needed to highlight the cell row only after satifsying all criteria
我想我的 For
IF
和 Next
语句的顺序有问题,我试图只突出显示满足所有条件的行,而不是当我的代码使其成为突出显示部分所有行都单独突出显示并且代码似乎 运行 相当慢,我相信我执行了太多迭代?
Sub SWAPS100()
Dim rng As Range, lCount As Long, LastRow As Long
Dim cell As Object
Sheets("Output").Activate
With ActiveSheet
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For Each cell In .Range("E2:E" & LastRow) 'new position
If cell = "N" Then
Debug.Print
For Each cell1 In .Range("U2:U" & LastRow) 'Secuirty type
If cell1 = "SW" Then
For Each cell2 In .Range("J2:J" & LastRow) 'prior px
If cell2 = 100 Then
For Each cell3 In .Range("I2:I" & LastRow) 'current px
If cell3 <> 100 Then
'With cell.Interior
With cell.EntireRow.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 6382079
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next cell3
End If
Next cell2
End If
Next cell1
End If
Next cell
End With
正如@Raystafarian 在我打字时评论的那样,在你的 if 语句中使用 And
而不是所有循环:
Sub SWAPS100()
Dim rng As Range, lCount As Long, LastRow As Long
Dim cell As Object
Sheets("Output").Activate
With ActiveSheet
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For Each cell In .Range("E2:E" & LastRow) 'new position
If cell = "N" And cell.Offset(, 16) = "SW" And cell.Offset(, 5) = 100 _
And cell.Offset(, 4) = 100 Then
With cell.EntireRow.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 6382079
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next cell
End With
单独循环每一行它会变慢并且很可能总是合理的。只要你在每一列中有一个单元格来证明 if 语句,那么它就会为所有行着色。
也可以使用以下公式的条件格式来完成:
=AND($E2="N",$U2="SW",$J2=100,$I2=100)
虽然前面提到的 Conditional Formatting with a native worksheet formula is a better solution for 'on-the-fly' updates, a series of AutoFilter methods 应用于列会比涉及遍历单元格的任何过程快得多。
Sub SWAPS100()
Application.ScreenUpdating = False
With Sheets("Output")
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells(1, 1).CurrentRegion
.AutoFilter Field:=5, Criteria1:="N"
.AutoFilter Field:=9, Criteria1:=100
.AutoFilter Field:=10, Criteria1:=100
.AutoFilter Field:=21, Criteria1:="SW"
With .Resize(.Rows.Count - 1, 1).Offset(1, 4)
If CBool(Application.Subtotal(103, .Cells)) Then
.Cells.EntireRow.Interior.Color = 6382079
End If
End With
End With
If .AutoFilterMode Then .AutoFilterMode = False
End With
Application.ScreenUpdating = True
End Sub
我想我的 For
IF
和 Next
语句的顺序有问题,我试图只突出显示满足所有条件的行,而不是当我的代码使其成为突出显示部分所有行都单独突出显示并且代码似乎 运行 相当慢,我相信我执行了太多迭代?
Sub SWAPS100()
Dim rng As Range, lCount As Long, LastRow As Long
Dim cell As Object
Sheets("Output").Activate
With ActiveSheet
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For Each cell In .Range("E2:E" & LastRow) 'new position
If cell = "N" Then
Debug.Print
For Each cell1 In .Range("U2:U" & LastRow) 'Secuirty type
If cell1 = "SW" Then
For Each cell2 In .Range("J2:J" & LastRow) 'prior px
If cell2 = 100 Then
For Each cell3 In .Range("I2:I" & LastRow) 'current px
If cell3 <> 100 Then
'With cell.Interior
With cell.EntireRow.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 6382079
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next cell3
End If
Next cell2
End If
Next cell1
End If
Next cell
End With
正如@Raystafarian 在我打字时评论的那样,在你的 if 语句中使用 And
而不是所有循环:
Sub SWAPS100()
Dim rng As Range, lCount As Long, LastRow As Long
Dim cell As Object
Sheets("Output").Activate
With ActiveSheet
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For Each cell In .Range("E2:E" & LastRow) 'new position
If cell = "N" And cell.Offset(, 16) = "SW" And cell.Offset(, 5) = 100 _
And cell.Offset(, 4) = 100 Then
With cell.EntireRow.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 6382079
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Next cell
End With
单独循环每一行它会变慢并且很可能总是合理的。只要你在每一列中有一个单元格来证明 if 语句,那么它就会为所有行着色。
也可以使用以下公式的条件格式来完成:
=AND($E2="N",$U2="SW",$J2=100,$I2=100)
虽然前面提到的 Conditional Formatting with a native worksheet formula is a better solution for 'on-the-fly' updates, a series of AutoFilter methods 应用于列会比涉及遍历单元格的任何过程快得多。
Sub SWAPS100()
Application.ScreenUpdating = False
With Sheets("Output")
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells(1, 1).CurrentRegion
.AutoFilter Field:=5, Criteria1:="N"
.AutoFilter Field:=9, Criteria1:=100
.AutoFilter Field:=10, Criteria1:=100
.AutoFilter Field:=21, Criteria1:="SW"
With .Resize(.Rows.Count - 1, 1).Offset(1, 4)
If CBool(Application.Subtotal(103, .Cells)) Then
.Cells.EntireRow.Interior.Color = 6382079
End If
End With
End With
If .AutoFilterMode Then .AutoFilterMode = False
End With
Application.ScreenUpdating = True
End Sub