UDF help

Sylvan

New Member
Joined
Nov 11, 2010
Messages
29
Kind of tricky to explain. I've written a UDF to sum bolded values until the next cell with a fill color is encountered. It works fine until it's copied down as part of a range and then I get a #VALUE! error. I'll then enter some values and it starts working. But then when I run a macro to paste a range below the UDF it gives me the #VALUE! error again.

Here's the code -- any ideas? Thanks in advance

Function ROOMSUM(MyRange As Range) As Double
'Sums bolded values down until the next colored cell

Dim rCell As Range

For Each rCell In MyRange
If rCell.Interior.ColorIndex <> xlNone Then Exit Function
If rCell.Font.Bold = True Then
ROOMSUM = ROOMSUM + rCell.Value
End If
Next rCell

End Function
 
Thanks shg. Actually the currency was only in there for a second, just happened to be the second when I copied and pasted.

The debug in every case is giving me a cell 2 down from the UDF which contains the formula =L21*M21 the result of which is $0.00 until data is entered and then everything is fine. So how do I get it to recognize the 0 as a number?
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I assume there are no merged cells.

When the code stops, in the Immediate window, enter

? VarType(rCell)

What's that show?
 
Upvote 0
No, no merged cells.

When I enter ? VarType(rCell) it gives me a 6. If I enter ? VarType(ThisCell) I get a 0.
 
Upvote 0
That means the cell is Empty, which probably means it hasn't recalculated yet. See http://www.decisionmodels.com/calcsecretsj.htm#value

So just to see what happen, try this:

Code:
Function ROOMSUM(MyRange As Range) As Double
    Dim rCell     As Range
 
    On Error GoTo Oops
    For Each rCell In MyRange
        With rCell
            If .Interior.ColorIndex <> xlColorIndexNone Then Exit Function
            If .Font.Bold = True Then
                If VarType(rCell.Value2) = vbDouble Then
                    ROOMSUM = ROOMSUM + rCell.Value2
                ElseIf Not IsEmpty(cell.Value2) Then
                    GoTo Oops
                End If
            End If
        End With
    Next rCell
    Exit Function
 
Oops:
    Debug.Print Application.ThisCell.Address, rCell.Address
    Stop
End Function
 
Upvote 0
Post a minimal workbook that illustrates the problem on box.net or similar.

Did you fix the typo in the ElseIf line to rCell?
 
Upvote 0
It's the Interior property that's causing the error. My only suggestion is to punt on RoomSum and use this formula in N12:

=SUMPRODUCT(N13:INDEX(N:N, ROW() + IF(COUNTIF(A13:A977, "New Room"), MATCH("New Room", A13:A977, 0), 1000) - 1))
 
Upvote 0
Hmm, ok thanks for all your help. That formula doesn't really work because I only want to add the cells in column N that are bolded -- as you have it, it sums everything until the next row with "New Room".
 
Upvote 0

Forum statistics

Threads
1,224,621
Messages
6,179,938
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top