0

我必须创建一个类似的单元格,并且我正在寻找一种使用 excel 公式或 VBA 代码来完成它的方法。

在此处输入图像描述

有两种不同的形式redgreen或只是black,但并非在所有情况下都取决于 If。

例如:

情况 1:如果单元格 F1 和 G1 具有值,则格式为红色到绿色

情况 2:如果单元格 F1 有值但 G 为空,则图像 1 中的最终格式必须为黑色

在此处输入图像描述

4

1 回答 1

1

这个 VBA 代码应该可以工作。它进入worksheet_change您的工作表事件。

我只是将它设置为 I 列以更改颜色,但如果需要,您可以扩展到 J 和 K。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

If Target.Column = 6 Or Target.Column = 7 Then

    Select Case Target.Column

        Case Is = 6

            If Target <> vbNullString And Target.Offset(, 1) <> vbNullString Then

                With Target.Offset(, 2)
                    .Characters(Start:=1, Length:=5).Font.ColorIndex = 3
                    .Characters(Start:=6, Length:=3).Font.ColorIndex = 4
                End With

            Else: Target.Offset(, 2).Font.ColorIndex = 0

            End If

        Case Is = 7

            If Target <> vbNullString And Target.Offset(, -1) <> vbNullString Then

                With Target.Offset(, 1)
                    .Characters(Start:=1, Length:=5).Font.ColorIndex = 3
                    .Characters(Start:=6, Length:=3).Font.ColorIndex = 4
                End With

            Else: Target.Offset(, 1).Font.ColorIndex = 0

            End If

    End Select

End If

With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

End Sub
于 2013-02-22T14:32:48.637 回答