+ Reply to Thread
Results 1 to 6 of 6

Problem with VBA formula to sum cell's based on background color

  1. #1
    Registered User
    Join Date
    07-24-2023
    Location
    Florian?polis, Brazil
    MS-Off Ver
    Office 365
    Posts
    3

    Problem with VBA formula to sum cell's based on background color

    Good afternoon,

    I have a problem with a VBA function to sum cells based on background color. I got the function from a site calle trumpexcell, this is it:

    'Code created by Sumit Bansal - (Not posting the link because im a new member)
    'This VBA code created a function that can be used to sum cells based on color
    Function SumByColor(SumRange As Range, SumColor As Range)
    Dim SumColorValue As Integer
    Dim TotalSum As Long
    SumColorValue = SumColor.Interior.ColorIndex
    Set rCell = SumRange
    For Each rCell In SumRange
    If rCell.Interior.ColorIndex = SumColorValue Then
    TotalSum = TotalSum + rCell.Value
    End If
    Next rCell
    SumByColor = TotalSum
    End Function


    In my case one sum is giving 39,999.00 but the three cells it is counting have 13,333.33+18,683.33+7,983.34 and it sums 40,000.00 if i do normally
    Why is it behaving like this and what should I do for it?
    Appreciate for the help and sorry that english is not my first language

    Best regards,

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Problem with VBA formula to sum cell's based on background color


    Hello,

    it's what happens when using a bad variable data type !
    As the variable used for round numbers but your values are not as they contain decimal …

  3. #3
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Problem with VBA formula to sum cell's based on background color

    Please Login or Register  to view this content.
    Use Integer or Long variables for positive or negative Whole Numbers.
    Use Single or Double variables for "Floating point operations" (decimal values).

    Data Type Summary HERE
    Last edited by leelnich; 07-24-2023 at 09:35 AM.
    Clicking the Add Reputation star below helpful posts is a great way to show your appreciation.
    Please mark your threads as SOLVED upon conclusion (Thread Tools above Post # 1). - Lee

  4. #4
    Registered User
    Join Date
    07-24-2023
    Location
    Florian?polis, Brazil
    MS-Off Ver
    Office 365
    Posts
    3

    Re: Problem with VBA formula to sum cell's based on background color

    Thanks for the fast reply leelnich, appreciate it

    So as I'm using it to count currency, I should chande the Integer to Single and the Long to Double in the function I sent above?

    Just to see if i got it,

    Best Regarfs,

  5. #5
    Registered User
    Join Date
    07-24-2023
    Location
    Florian?polis, Brazil
    MS-Off Ver
    Office 365
    Posts
    3

    Re: Problem with VBA formula to sum cell's based on background color

    Thanks for the fast reply Marc L, appreciate it

    So as I'm using it to count currency, I should chande the Integer to Single and the Long to Double in the function as Leenich said?

    Sorry if its a dumb question,

    Best Regards,

  6. #6
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Problem with VBA formula to sum cell's based on background color

    Generally speaking, if it's a whole number, stick with Long; that's VBA's native whole-number format.
    If it's a fractional/decimal number, go with Double, which matches Excel's native number type.

    (Using native types in mathematical operations is slightly faster because VBA doesn't have to convert first.)

    So, assuming ColorIndex is a whole number, use Long and Double.

    If that concludes your question, please go to Thread Tools above post #1 and mark your thread as SOLVED.
    And since you're new . . .
    Wholly at your discretion, clicking the Add Reputation stars below helpful posts is a great way to acknowledge those who offered solutions. - Lee
    Last edited by leelnich; 07-25-2023 at 03:18 PM.

+ 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. change background textbox color based on text color in cell
    By atadaliran in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2021, 05:34 AM
  2. Replies: 8
    Last Post: 04-02-2016, 04:00 AM
  3. How to add value to cell based on background color
    By Anh Trinh in forum Excel General
    Replies: 2
    Last Post: 04-15-2015, 05:00 AM
  4. Change cell background color based on another cells background color
    By Queo in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 4
    Last Post: 06-10-2014, 05:28 AM
  5. Replies: 4
    Last Post: 09-15-2012, 02:46 PM
  6. Set background color based on cell value
    By tgal in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-29-2011, 06:30 AM
  7. formula based on cell background color
    By seyah in forum Excel General
    Replies: 2
    Last Post: 05-11-2007, 05:20 PM

Tags for this Thread

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