При попытке сделать ячейку изменить цвета на основе значения другой ячейки - PullRequest
0 голосов
/ 19 февраля 2020

Я пытаюсь изменить цвет объединенной ячейки (C2) с красного на зеленый в зависимости от значения другой ячейки (E2), и я не получаю сообщение об ошибке, но оно также не работает. Ячейки должны быть переменными строками, потому что я делаю шаблон для ввода данных. Если ячейка E2 имеет 0, тогда C2 должен стать красным, а если E2 имеет 1, то C2 должен стать зеленым

Я попытался выделить все переменные в моей проблеме и построчно комментируя их чтобы изолировать актуальную проблему. Я также указал лист, на котором были включены столбцы и диапазоны, но он ничего не делал, хотя и не выдавал ошибку. Я получил кое-что для работы на тестовом листе, но это было не с переменными строками.

Код, который я выделил, -

Sub setCondFormat()

topRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'chooses the first row of the next template input
bottomRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(10, 0).Row
'chooses the last row of the next template input
stageOneEnd = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(2, 0).Row
'sets end parameter for the first stage cell

Sheet1.Range("C" & topRow & ":C" & stageOneEnd).Merge
Sheet1.Range("C" & topRow & ":C" & stageOneEnd).VerticalAlignment =     xlCenter
Sheet1.Range("C" & topRow & ":C" & stageOneEnd).HorizontalAlignment =     xlCenter
'Start merginging and center column C

Sheet1.Range("E" & topRow & ":E" & bottomRow).Merge
Sheet1.Range("E" & topRow & ":E" & bottomRow).VerticalAlignment = xlCenter
Sheet1.Range("E" & topRow & ":E" & bottomRow).HorizontalAlignment =     xlCenter
'End merging and center column E

Sheet1.Range("C" & topRow & "").Interior.ColorIndex = 3
'sets up default color for the C column which is red (stage)

Sheet1.Range("C" & topRow & ":C" & stageOneEnd).Value = "Picture Taken"
'inputs label into the first stage (Picture Taken)

Sheet1.Range("E" & topRow & "").Value = 0
' inputs a zero value into column E to ensure first stage of column C starts off as red
    Sheet1.Range("E2").Select
    'selects cell E2, I think this is not important at all but is used as a     gateway to conditional formatting
    With Sheet1.Range("C" & topRow & ":C" & bottomRow & "")
    'this selects the range that will change due to the conditional formatting
        .FormatConditions.Add Type:=xlExpression, Formula1:= _
           "IF(Sheet1.(E" & topRow & ")=0,FALSE,TRUE)"
           'the condition for which the stage will change
           ' if the top cell in column E is equal to 0 then the formatting     will not go through but if it isnt equal to zero then it will
            With .FormatConditions(.FormatConditions.Count)
                .SetFirstPriority
                With .Interior
                    .PatternColorIndex = xlAutomatic
                    .Color = 5287936
                    .TintAndShade = 0
                    ' the new formatting of cell in the C column
                End With
            End With
    End With
End Sub

Любая помощь очень ценится. Я был очень застрял на этом в течение некоторого времени. Заранее спасибо.

...