使用Excel VBA将特定单元格着色一个单元格

时间:2011-07-01 09:07:41

标签: excel-vba vba excel

我正在根据用户表单中的条目生成Excel工作表。一个条目是项目持续时间内的报告间隔。假设报告间隔为三个月,第二阶段的持续时间为10个月,第三阶段为三个月。

我的列结构如下,相位之间有一个单元格的距离:

Phase1: Starting phase
Phase2: Working phase
Phase3: Closing phase

Phase1  Phase2                      Phase3
||||||  ||||||||||||||||||||||||||| ||||||

报告间隔应在Phase2和Phase3中标记为彩色单元格,如下所示:

Phase1  Phase2                        Phase3
||||||  |||||||O|||||||||||O||||||||  ||O||||

这是我为细胞着色的代码:

For x = 1 To (Implementationduration + Closingduration - 1) / 3
            Select Case x
                Case Is < Implementationduration:
                    Call SetFontAndBackground(cells(Rowindex, Phase1CellLast() + Columncounter * 3), cells(Rowindex, Phase1CellLast() + Columncounter * 3), False, False, "", 10, False, "b", "lcyan", False)
                    Columncounter = Columncounter + 4
                Case Is > Implementationduration:
                    Call SetFontAndBackground(cells(Rowindex, Phase1CellLast() + Columncounter * 3 + 1), cells(Rowindex, Phase1CellLast() + Columncounter * 3 + 1), False, False, "", 10, False, "b", "lcyan", False)
                    Columncounter = Columncounter + 4
            End Select
Next x

问题是Phase3中的彩色单元格未正确定位。他们离开一个牢房。为了着色,我使用子程序来格式化单元格。我在代码中找不到错误。

1 个答案:

答案 0 :(得分:1)

发现问题。我的Select case语句不正确。它必须是:

For x = 1 To (Phase2duration + Phase3duration - 1) / 3
            Phase2range = Phase1CellLast() + Columncounter * 3
            Select Case Phase2range

                Case Is < Phase2CellLast():
                  Call SetFontAndBackground(cells(Rowindex, Phase1CellLast() + Columncounter * 3), cells(Rowindex, Phase1CellLast() + Columncounter * 3), False, False, "", 10, False, "b", "lcyan", False)
                    Columncounter = Columncounter + 4
                Case Is > Phase2CellLast():
                    Call SetFontAndBackground(cells(Rowindex, Phase1CellLast() + Columncounter * 3 + 1), cells(Rowindex, Phase1CellLast() + Columncounter * 3 + 1), False, False, "", 10, False, "b", "lcyan", False)
                    Columncounter = Columncounter + 4
            End Select
Next x