Consulting

Results 1 to 3 of 3

Thread: Count colored cells in a range using columns and rows, output result

  1. #1
    VBAX Newbie
    Joined
    Nov 2019
    Posts
    2
    Location

    Count colored cells in a range using columns and rows, output result

    Hi everyone. I'm pretty new to vba programing. I'm trying to count the colored cells (every cell that's not white) for each column and output the number result in cells DB4, DB5, DB6, DB7 ......etc. For example, the number of colored cells in range E4:E21 is printed in cell DB4, the number of colored cells in range F4:F21 is printed in cell DB5.....etc. For some reason when I click the button and run the macro the result is 18 for each value in the entire DB column. If I type =-4142 in place of <>-4142 (or <>xlNone) then the result is 0 for each value in the entire DB column (just wondering if this is where the mistake is). I've double checked that the plain cells are white with no color fill, and not just a different shade of white or something. If you opened up a new sheet in excel and filled some cells in E4:AI21 with color and ran the code I would like it to output the correct count for each column. It seems to output to the correct column B4, but doesn't output the correct counts. If someone could help me figure out why my code is not working I'd be really grateful! Microsoft Excel 2019. Thanks guys, I'm still learning here!



    Sub Procedure()
    
      Dim nRowIndex As Integer, nCellNumber As Integer, rng As Range
    
        r = 4
        For Each rng In Range("E:AI").Columns
        nCellNumber = 0
            For nRowIndex = 4 To 21
                If Range("rng" & nRowIndex).Interior.ColorIndex <> -4142 Then
                    nCellNumber = nCellNumber + 1
                Else
                    'do nothing
                End If
            Next nRowIndex
            If nCellNumber = 0 Then Range("DB" & r) = "0" Else Range("DB" & r) = nCellNumber
        r = r + 1
        Next rng
    
    End Sub

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    instead of
    If Range("rng" & nRowIndex).Interior.ColorIndex <> -4142 Then
    try:
    If rng.Cells(nRowIndex).Interior.ColorIndex <> -4142 Then
    Your code is looking repeatedly at cells RNG4:RNG21
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Newbie
    Joined
    Nov 2019
    Posts
    2
    Location
    Thanks a lot for your help, your code worked great! Hope you have a great holiday season!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •