跳过彩色的单元格

我有一个循环,我想跳过有颜色的单元格。

For i = 1 To Count Do While ActiveCell.Offset(0, i).Interior.ColorIndex = 15 i = i + 1: Count = Count + 1 Loop With ActiveCell.Offset(0, i).Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent3 .TintAndShade = -0.249977111117893 .PatternTintAndShade = 0 End With Next i 

它的工作,但是初始计数variables不更新。 因此,如果我有10并且有2次跳跃,那么i值会增加,即使variables为12count仍然保持在10 。 看来好像增加countvariables不会增加For循环。 我不能从ivariables中取1 ,因为这会导致activecell.offset受到影响。

为什么使用.Offset呢? 这是你正在尝试? 这样你也可以跳过彩色单元格。

 Dim col As Long, rw As Long, i As Long col = ActiveCell.Column rw = ActiveCell.Row For i = 1 To Count With Cells(rw, col + i) If .Interior.ColorIndex <> 15 Then With .Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent3 .TintAndShade = -0.249977111117893 .PatternTintAndShade = 0 End With End If End With Next i 

可能非常低效的代码,但没有太多的行。 记住这个想法是,如果该列是星期六或星期天,那么列就有一个date,即灰色,那么代码应该跳过这些单元格,而不是从整个计数器中减去它们。

 If Not IsEmpty(y.Value) And IsNumeric(y.Value) And y.Value >= 7.5 Then With ActiveCell.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent3 .TintAndShade = -0.249977111117893 .PatternTintAndShade = 0 End With Col = y.Value - 7.5 Col = Col / 7.5 Count = Left(Col, Len(Col) - InStr(1, Col, ".") + 1) y = 0 For i = 1 To Count Do While ActiveCell.Offset(0, i).Interior.ColorIndex = 15 ActiveCell.Offset(0, 1).Select y = y + 1 Loop With ActiveCell.Offset(0, i).Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent3 .TintAndShade = -0.249977111117893 .PatternTintAndShade = 0 End With Next i ActiveCell.Offset(0, -y).Select ActiveCell.Offset(0, i + y).Select Do While ActiveCell.Interior.ColorIndex = 15 ActiveCell.Offset(0, 1).Select Loop Co = Right(Col, Len(Col) - InStr(1, Col, ".")) If Len(Co) > 2 Then Co = Mid(Co, 1, InStr(1, Col, ".")) & "." & Mid(Co, InStr(1, Col, ".") + 1, Len(Co) - InStr(1, Col, ".")) End If If Co = 0 Then ElseIf Co >= 0.1 And Co <= 25 Then With ActiveCell.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorDark1 .TintAndShade = -4.99893185216834E-02 .PatternTintAndShade = 0 End With ElseIf Co >= 26 And Co <= 49 Then With ActiveCell.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent6 .TintAndShade = 0.799981688894314 .PatternTintAndShade = 0 End With ElseIf Co >= 5 And Co <= 74 Then With ActiveCell.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.799981688894314 .PatternTintAndShade = 0 End With ElseIf Co >= 75 And Co <= 99 Then With ActiveCell.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorDark1 .TintAndShade = -4.99893185216834E-02 .PatternTintAndShade = 0 End With End If End If Next y