计算特定列表中没有多少个有效单元格

时间:2016-01-22 19:35:03

标签: excel vba

我想计算最后列表中没有多少个值的单元格,并反映出MsgBox中的数字。

以下是Xavier Navarro的代码

Sub CheckDropDown()
Dim MyStringVar As Variant, i As Integer
Dim Lookup_Range, cel As Range, lastRow As Integer, check As Boolean
Set Lookup_Range = Worksheets("Lists").Range("C1:C21")
lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
    For Each cel In Lookup_Range
        If ActiveSheet.Cells(i, 25) = cel.Value Then
            check = True: Exit For
        Else
            check = False
        End If
    Next
    If check Then
        'The cell is on the lookupRange
    Else
        'The cell is NOT on the lookupRange
        ActiveSheet.Cells(i, 25).Interior.Color = RGB(255, 255, 5)
    End If
Next i
End Sub

1 个答案:

答案 0 :(得分:0)

    Sub CheckDropDown()
    Dim MyStringVar As Variant, i As Integer, counter As Integer 'declare variable
    Dim Lookup_Range, cel As Range, lastRow As Integer, check As Boolean
    counter = 0
    Set Lookup_Range = Worksheets("Lists").Range("C1:C21")
    lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
    For i = 2 To lastRow
        For Each cel In Lookup_Range
            If ActiveSheet.Cells(i, 25) = cel.Value Then
                check = True: Exit For
            Else
                check = False
            End If
        Next
        If check Then
            'The cell is on the lookupRange
        Else
            'The cell is NOT on the lookupRange
        counter = counter + 1 'increment when not found
            ActiveSheet.Cells(i, 25).Interior.Color = RGB(255, 255, 5)
        End If
    Next I
    MsgBox counter 'output message
    End Sub