如何在Workbook_BeforeSave中的单元格中填充颜色

时间:2016-07-13 09:06:31

标签: excel vba excel-vba

我目前有以下代码:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

ThisWorkbook.Sheets("Sheet1").Cells(1, 1).Select

For i = 2 To 10
    If ThisWorkbook.Sheets("Sheet1").Cells(i, 1).Value = "YES" Then
        For j = 2 To 5
            If ThisWorkbook.Sheets("Sheet1").Cells(i, j).Value = "" Then
                **<insert format here>**
            End If
        Next j
    End If


    If ThisWorkbook.Sheets("Sheet1").Cells(i, 1).Value = "NO" Then
        For j = 2 To 5
            If ThisWorkbook.Sheets("Sheet1").Cells(i, j).Value = "" Then
                **<insert format here>**
            End If
        Next j
    End If
Next i

End Sub

现在。我试图在If语句中添加下面的代码,但没有工作

ThisWorkbook.Sheets("Sheet1").Cells(i, j).Interior.ColorIndex = 3

如何在没有值的情况下为细胞添加填充颜色?

2 个答案:

答案 0 :(得分:2)

Interior.ColorIndex必须适用于范围

可能是没有按预期工作的逻辑

试试这个

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim i As Long

    With ThisWorkbook.Sheets("Sheet1")
        For i = 2 To 10
            Select Case .Cells(i, 1).value
                Case "YES"
                    If WorksheetFunction.CountA(.Cells(i, 2).Resize(, 4)) < 4 Then .Cells(i, 2).Resize(, 4).SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 3
                Case "NO"
                    If WorksheetFunction.CountA(.Cells(i, 2).Resize(, 4)) < 4 Then .Cells(i, 2).Resize(, 4).SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 2
            End Select
        Next i
    End With
End Sub

答案 1 :(得分:1)

试试这个(保存前):

NavigationView navigation = (NavigationView)getActivity().findViewById(R.id.navigation_view);
Menu drawer_menu = navigation.getMenu();
MenuItem menuItem;
menuItem = drawer_menu.findItem(R.id.Id_ofYourMenuItem);
    if(!menuItem.isChecked())
    {
        menuItem.setChecked(true);
    }