如果每张纸上的K1为>,则尝试让VBA循环通过纸张和颜色标签为红色。 0.到目前为止我没有在工作表中循环,只是颜色活动表:
Sub IfJNegRedTab()
'
'
'
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
Range("K1").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-1],""<0"")"
If Range("K1").Value > 0 Then
With ActiveWorkbook.ActiveSheet.Tab
.Color = 255
.TintAndShade = 0
End With
End If
Next sht
'
End Sub
答案 0 :(得分:1)
始终确保您引用了预期的表格。简单地Range(A1)
将引用活动表。你需要做Sheets("MySheet").Range(A1)
。请注意,这也是avoid using .Select
/.Activate
Sub IfJNegRedTab()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
With sht
.Range("K1").FormulaR1C1 = "=COUNTIF(C[-1],""<0"")"
If .Range("K1").Value > 0 Then
With .Tab
.Color = 255
.TintAndShade = 0
End With
End If
End With
Next sht
End Sub
还可以删除K1
中仅用于检查列左侧值的公式:
Sub IfJNegRedTab_v2()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
With sht
If .Range("K1").Offset(0, -1).Value < 0 Then
With .Tab
.Color = 255
.TintAndShade = 0
End With
End If
End With
Next sht
End Sub