值更改时单元格颜色也会更改

时间:2018-11-09 07:38:44

标签: excel vba excel-vba

enter image description here

我正在研究VBA程序。我创建了一个函数,该函数计算满足特定结果的项目的数量(大小“大”和食物“蔬菜”)。该功能有助于计算满足要求的项目数,并在我根据类别过滤时进行更改(例如p1有1个蔬菜,它显示1)。

Function Count_items_Small_Veg() As Long

Dim i As Long
Dim r As Range
Dim wsCheck As Worksheet
Dim n As Integer
    Application.Volatile
    Set wsCheck = Application.Caller.Parent
    For Each r In wsCheck.AutoFilter.Range.Columns(Col_Western).Cells
        If wsCheck.Cells(r.Row, Col_Starter) = "S" And _
                wsCheck.Cells(r.Row, Col_Dessert) = "VEGETARIAN" And r.EntireRow.Hidden = False Then
            i = i + 1
        End If
    Next r
    Count_items_Small_Veg = i

End Function

当我过滤结果时,我希望程序将存储数值的单元格更改为灰色(当达到零时为灰色)。例如,我根据P2和大蔬菜的0阶过滤。它应该是灰色的

The grey cell is desired result when cell value equals to 0

Private Sub Worksheet_Change(ByVal Target As Range)

Dim ChangeRng As Range

Set ChangeRng = ActiveSheet.Range("A5:G6")

Application.Volatile
For Each cell In ChangRng
If cell.Value = 0 Then
cell.Interior.ColorIndex = 16
End If

End Sub

0 个答案:

没有答案