+ Reply to Thread
Results 1 to 7 of 7

Conditional formatting UDF value error

Hybrid View

YasserKhalil Conditional formatting UDF... 11-09-2015, 03:22 PM
skywriter Re: Conditional formatting... 11-09-2015, 04:19 PM
TMS Re: Conditional formatting... 11-09-2015, 04:38 PM
YasserKhalil Re: Conditional formatting... 11-09-2015, 05:19 PM
PCI Re: Conditional formatting... 11-09-2015, 05:13 PM
YasserKhalil Re: Conditional formatting... 11-09-2015, 05:16 PM
PCI Re: Conditional formatting... 11-09-2015, 05:43 PM
  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Conditional formatting UDF value error

    Hello everyone
    I have UDF function that enables me to count colored cells (conditional formatting cells) and sum the values of colored cells
    Function CountCFCells(Rng As Range, C As Range, bCount As Boolean)
        Dim I As Single, J As Long
        Dim Chk As Boolean, Str1 As String, CFCELL As Range
        Application.Volatile
        Chk = False
        
        For I = 1 To Rng.FormatConditions.Count
            If Rng.FormatConditions(I).Interior.ColorIndex = C.Interior.ColorIndex Then
                Chk = True
                Exit For
            End If
        Next I
        
        J = 0
        If Chk = True Then
            For Each CFCELL In Rng
                Str1 = CFCELL.FormatConditions(I).Formula1
                Str1 = Application.ConvertFormula(Str1, xlA1, xlR1C1)
                Str1 = "=" & CFCELL.Address & Split(Str1, "]")(UBound(Split(Str1, "]")))
                If bCount = False Then
                    If Evaluate(Str1) = True Then J = J + 1
                Else
                    If Evaluate(Str1) = True Then J = J + CFCELL
                End If
            Next CFCELL
        Else
            CountCFCells = "Color Not Found"
            Exit Function
        End If
        CountCFCells = J
    End Function
    I need to update results of the UDF .. as I noticed the following ..if I select any cell withing A1:B15 which has conditional formatting rules, the results give a value error ..but when selecting any other cell , everything goes right..
    Can I fix this debug?
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: Conditional formatting UDF value error

    Maybe this.
    Here's the formula I used in cell G3 of your sample sheet.
    Formula: copy to clipboard
    =CountIF(B1:B15,"<=20")+CountIF(A1:A15,">=20")

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,251

    Re: Conditional formatting UDF value error

    Actually, the problem occurs if you select a cell anywhere in column A or column B.

    A workaround is to add this near the start of your code:

    If ActiveCell.Column <= 2 Then
        CountCFCells = "error"
        Exit Function
    End If

    It doesn't make the problem go away ... but you could change the error message to say "don't select this cell!"

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Conditional formatting UDF value error

    Mr. PCI
    That's awesome .. really fascinating
    Exactly as needed
    Thank you very much for great help

  5. #5
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Conditional formatting UDF value error

    See next code
    Option Explicit
    
    Function CountCFCells(Rng As Range, C As Range, bCount As Boolean)
        Dim I As Single, J As Long
        Dim Chk As Boolean, Str1 As String, CFCELL As Range
        Application.Volatile
        Chk = False
        
        For I = 1 To Rng.FormatConditions.Count
            If Rng.FormatConditions(I).Interior.ColorIndex = C.Interior.ColorIndex Then
                Chk = True
                Exit For
            End If
        Next I
        
        J = 0
        If Chk = True Then
            For Each CFCELL In Rng
                Str1 = CFCELL.FormatConditions(I).Formula1
                Dim II As Integer
                Dim IIFlg As Boolean
                Dim Tmp
                IIFlg = False
                For II = 1 To Len(Str1)
                   Tmp = Mid(Str1, II, 1)
                   If ("0123456789" Like "*" & Tmp & "*") Then
                      IIFlg = True
                   Else
                      If (IIFlg) Then Exit For
                   End If
                Next
                Tmp = Right(Str1, Len(Str1) - II + 1)
                Str1 = "=" & CFCELL.Address & Tmp
                If bCount = False Then
                    If Evaluate(Str1) = True Then J = J + 1
                Else
                    If Evaluate(Str1) = True Then J = J + CFCELL
                End If
            Next CFCELL
        Else
            CountCFCells = "Color Not Found"
            Exit Function
        End If
        CountCFCells = J
    End Function
    - Battle without fear gives no glory - Just try

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Conditional formatting UDF value error

    Thanks Mr. Bruce for nice formula
    In fact I know that I can achieve counting using Countif function ..but in original file the issue is very complicated so I use this UDF function

    Thank you very much Mr. TMS for this addition to code but I need to deal with the UDF in normal way
    That's I need to select any cell in any column without that error ..Just to display or update the results at once

  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: Conditional formatting UDF value error

    YasserKhalil you are welcome
    It was a pleasure, to understand the issue and to prepare the update

+ 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. Conditional Formatting - Run Time Error '13' Type Mismatch Error
    By ksp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-17-2011, 07:37 PM
  2. Conditional formatting error
    By graeme.hilton@gmail.com in forum Excel General
    Replies: 4
    Last Post: 05-22-2006, 10:25 AM
  3. Conditional Formatting error
    By Nikki in forum Excel General
    Replies: 1
    Last Post: 03-10-2006, 07:35 AM
  4. conditional formatting for Error
    By krupesh in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 PM
  5. conditional formatting for Error
    By Bob Phillips in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-06-2005, 02:05 PM
  6. [SOLVED] conditional formatting for Error
    By krupesh in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 11:05 AM
  7. [SOLVED] conditional formatting for Error
    By krupesh in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  8. conditional formatting for Error
    By krupesh in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-04-2005, 02:05 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