VBA Help - 2 decimal places

Alex501

Board Regular
Joined
Dec 11, 2015
Messages
59
Office Version
  1. 365
  2. 2019
Hi

Can someone help me with some VBA code, I have the following to sum cells based on background colour;


  1. Function SumByColor(CellColor As Range, rRange As Range)
  2. Dim cSum As Long
  3. Dim ColIndex As Integer
  4. ColIndex = CellColor.Interior.ColorIndex
  5. For Each cl In rRange
  6. If cl.Interior.ColorIndex = ColIndex Then
  7. cSum = WorksheetFunction.SUM(cl, cSum)
  8. End If
  9. Next cl
  10. SumByColor = cSum
  11. End Function
The above only sums whole numbers, how do I amend it so that it will sum to 'X' amount of decimal places. I know I need to add ROUND into it but my attempts have failed.

Thanks

Alex
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi,
cSum is Declared as a Long Integer - Try changing it to Double and see if this does what you want

Rich (BB code):
Function SumByColor(CellColor As Range, rRange As Range) As Double
    Dim cSum As Double
    Dim ColIndex As Integer
    ColIndex = CellColor.Interior.ColorIndex
    For Each cl In rRange
    If cl.Interior.ColorIndex = ColIndex Then
    cSum = WorksheetFunction.Sum(cl, cSum)
    End If
    Next cl
    SumByColor = cSum
End Function

Also, maybe a good idea for the Function to return the same

Dave
 
Upvote 0
Hi,
Try this:
VBA Code:
Function SumByColor(CellColor As Range, rRange As Range, Optional num_digits = 2)
  Dim cl As Range
  Dim cSum As Double
  Dim ColIndex As Integer
  ColIndex = CellColor.Interior.ColorIndex
  For Each cl In rRange
    If cl.Interior.ColorIndex = ColIndex Then
      cSum = cSum + cl.Value
    End If
  Next cl
  SumByColor = WorksheetFunction.Round(cSum, num_digits)
End Function
The optional 3d parameter is the number of digits (decimal places) for rounding, if omitted then it is equal to 2 digits
 
Upvote 0
Hi,
Try this:
VBA Code:
Function SumByColor(CellColor As Range, rRange As Range, Optional num_digits = 2)
  Dim cl As Range
  Dim cSum As Double
  Dim ColIndex As Integer
  ColIndex = CellColor.Interior.ColorIndex
  For Each cl In rRange
    If cl.Interior.ColorIndex = ColIndex Then
      cSum = cSum + cl.Value
    End If
  Next cl
  SumByColor = WorksheetFunction.Round(cSum, num_digits)
End Function
The optional 3d parameter is the number of digits (decimal places) for rounding, if omitted then it is equal to 2 digits

Works like a charm, thank you.
 
Upvote 0
You are welcome, glad it helped.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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