+ Reply to Thread
Results 1 to 2 of 2

#VALUE error when counting cells

Hybrid View

  1. #1
    Registered User
    Join Date
    02-27-2019
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    70

    #VALUE error when counting cells

    Hello,

    I use the code below to count cells with a certain fill color and when they are empty.
    When i try this in an empty sheet it works perfectly but when i implement this into a big sheet where the sollution is for i get a #VALUE error.
    There is no error in the range so that cannot be the problem. I know it is hard to find a sollution when the real workbook isn't provided but i realy can't upload that.
    Does anyone know what can trigger this error? I added a example sheet if the cell count sollution isn't clear to anyone.

    TX!

    Public Function CountCells(RNG As Range) As Double
        Application.Volatile
    For Each CL In RNG
        If IsEmpty(CL) = True Then
            With CL.Interior
                If .Pattern = xlSolid And .Color = RGB(146, 205, 220) Then
                    CountCells = CountCells + 1
                End If
            End With
        End If
    Next CL
    
    
    End Function
    Attached Files Attached Files

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: #VALUE error when counting cells

    Hi there,

    It's almost impossible to suggest a solution without access to the workbook which contains the error, but the following code might provide a workaround in cases where the error is in fact generated by the data values:

    
    
    
    Option Explicit
    
    
    Public Function CountCells(rTarget As Range) As Long
    
        Dim rCell As Range
    
        Application.Volatile
    
        CountCells = 0
    
        For Each rCell In rTarget.Cells
    
            If IsError(rCell.Value) = False Then
    
                If rCell.Value = vbNullString Then
    
                    With rCell.Interior
    
                        If .Pattern = xlSolid And _
                           .Color = RGB(146, 205, 220) Then
    
                            CountCells = CountCells + 1
    
                        End If
    
                    End With
    
                End If
    
            End If
    
        Next rCell
    
    End Function

    Hope this helps,

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Overflow error on worksheet_selectionchange when counting selected cells
    By Spritz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-04-2021, 01:05 PM
  2. Replies: 3
    Last Post: 08-30-2018, 11:13 PM
  3. [SOLVED] Error counting cells by colour
    By Tony0731 in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 07-27-2018, 08:47 AM
  4. [SOLVED] COUNTIFS - Counting in order in one column, Counting only specific cells In another
    By kslattery in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-25-2017, 01:17 PM
  5. [SOLVED] Counting number of non-blank cells in column......but getting run time error in vba
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-30-2012, 10:54 AM
  6. Replies: 3
    Last Post: 03-01-2012, 06:17 AM
  7. counting cells with data without counting duplicates
    By labettis in forum Excel General
    Replies: 2
    Last Post: 11-05-2007, 12:10 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1