跳过有色的细胞

时间:2016-08-11 10:14:09

标签: excel vba excel-vba

我有一个循环,我想跳过有色的细胞。

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

它有效,但初始计数变量未更新。因此,如果我有102次跳过,则i值会增加且有效,但count仍为10,即使变量显示12。似乎增加count变量不会增加For循环。我无法将1i变量移开,因为这会导致activecell.offset受到影响。

2 个答案:

答案 0 :(得分:3)

为什么要使用.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

答案 1 :(得分:0)

可能效率非常低的代码,但没有很多行。请记住,该列具有日期,如果该列是星期六或星期日,即灰色,则代码应跳过这些单元格,但不从整个计数器中减去它们。

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