Coloring specific cells with Excel VBA off by one cell
I'm generating an Excel worksheet based on entries in a userform. One entry is a reporting interval over a project durat开发者_Python百科ion. Let's say the reporting interval is three months and the duration of Phase2 is 10 months and Phase3 three months.
My columns are structured like this with the distance of one cell between phases:
Phase1: Starting phase
Phase2: Working phase
Phase3: Closing phase
Phase1 Phase2 Phase3
|||||| ||||||||||||||||||||||||||| ||||||
The reporting intervals should be marked as colored cells in Phase2 and Phase3 like this:
Phase1 Phase2 Phase3
|||||| |||||||O|||||||||||O|||||||| ||O||||
This is my code to color the cells:
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
Problem is that the colored cells in Phase3 are not correctly positioned. They are off one cell. For coloring I use a subroutine for formatting the cells. I cannot find the mistake in my code.
Found the problem. My Select case statements were incorrect. It has to be:
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
精彩评论