【问题标题】:Coloring specific cells with Excel VBA off by one cell使用 Excel VBA 为特定单元格着色一个单元格
【发布时间】:2011-07-01 09:07:40
【问题描述】:

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

我的列的结构是这样的,相位之间有一个单元格的距离:

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 中的彩色单元格没有正确定位。他们离开了一个牢房。对于着色,我使用一个子程序来格式化单元格。我在我的代码中找不到错误。

【问题讨论】:

    标签: excel vba


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 2011-08-17
      相关资源
      最近更新 更多