# Problem with using countbycolor vba code

#### dester39

##### New Member
All,
When using this code the results for summing the cells values will not show up with a decimal point.
Example: If cell A1=1, cell B1=2.25 the total in C1 should be 3.25 but it only sums as 3.

How can I get the code to allow for decimal point values? Is this a formatting issue?

Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If SUM = True Then
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = WorksheetFunction.SUM(rCell, vResult)
End If
Next rCell
Else
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
End If
ColorFunction = vResult
End Function

### Excel Facts

Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

#### tygrrboi

##### Well-known Member
have you tried defining vResult as Double?

#### dester39

##### New Member
How would I add that into the above code?

#### tygrrboi

##### Well-known Member
Dim vResult

use
Dim vResult as Double

#### Trebor76

##### Well-known Member
Assuming you're using this User Defined Function it works for me

Have you passed in each parameter the function needs even the optional SUM Boolean argument??

I filled cell A1 green then put some numbers (including decimals) in A3:A8 and coloured some of those green and used the UDF like so...

=ColorFunction(A1,A3:A8,TRUE)

...and it worked fine. You may just need to format the output for decimals.

HTH

Robert

#### dester39

##### New Member
Sorry but that didn't help. Any other ideas?

#### dester39

##### New Member
trebor76
That worked thank you!

#### Trebor76

##### Well-known Member
trebor76
That worked thank you!

I didn't really do too much but you're welcome

Replies
1
Views
438
Replies
7
Views
367
Replies
17
Views
608
Replies
9
Views
349
Replies
2
Views
196

1,195,582
Messages
6,010,577
Members
441,557
Latest member
Jbest23

### 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.

### Which adblocker are you using?

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

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