如何使用ThisWorkbook BeforeSave替换Sheet1中的现有格式

时间:2016-07-18 08:09:39

标签: excel vba excel-vba

我目前有以下代码,只要单元格为空,就会执行格式化。

If cell is NULL
   Fill Green
If cell is not NULL
   Remove Fill

For i = 1 To 9


 With Target.Offset(0, i)
                    .Locked = False
                    .FormatConditions.Delete
                    .Value = ""
                    .FormatConditions.Add Type:=xlExpression, Formula1:="=ISBLANK(" & Target.Offset(0, i).Address & ")"
                    With .FormatConditions(.FormatConditions.Count)
                        .SetFirstPriority
                        .Interior.ColorIndex = 4
                    End With
                End With
            Next i

现在我的问题是,当用户在必填单元格(绿色单元格)上保存缺少值的excel文档时,我想将单元格的颜色从GREEN更改为RED

我在ThisWorkbook中有这段代码。

For j = 2 To 10
     If ThisWorkbook.Sheets("Sheet1").Cells(i, j).Value = "" Then
                *'ThisWorkbook.Sheets("Sheet1").Cells(i, j).Interior.ColorIndex = 3*
                errcnt = errcnt + 1
     End If
Next j

此代码ThisWorkbook.Sheets("Sheet1").Cells(i, j).Interior.ColorIndex = 3目前无效。我还尝试在此代码之前添加ThisWorkbook.Sheets("Sheet1").Cells(i, j).FormatConditions.Delete,但这仍然无法正常工作

1 个答案:

答案 0 :(得分:0)

以下代码将从单元格中删除现有的条件格式:

With Target.Offset(0, i)
    .Select
    Selection.FormatConditions.Delete
End With