2016-08-11 51 views
3

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

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 

它可以工作,但是初始计数变量没有更新。因此,如果我有10并且有2跳过,则i值会增加,并且该值仍然有效,但count仍然为10,即使变量为12。看起来好像增加count变量不会增加For循环。我不能把1i变量,因为这导致activecell.offset受到影响。

+0

我正确地说你试图找到最后一个颜色索引为15的单元格,然后改变下一个单元格的颜色吗? – Brian

+3

for循环是预编译的,启动后无法更改。你需要使用While循环来增加计数 – DragonSamu

+0

[在循环中更改For循环的长度]的可能重复(http://stackoverflow.com/questions/19409644/change-length-of-for-loop同时在循环) – DragonSamu

回答

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 
+0

Siddhart,像往常一样,你提供了一个很好的答案!我不得不保留偏移量的原因,如果因为可能有余数,需要以不同的颜色。从本质上讲,我创建了一个计划人员,它将数字分成7.5个等级(一个工作日),然后通过分部的答案来抵消油漆,而其余部分的油漆颜色会不同。请参阅下面的我的错误答案。 – Lowpar

+0

我相信,你仍然不需要'.Offset' :) –

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