+ Reply to Thread
Results 1 to 10 of 10

Sum by Colour Vba clarification please

  1. #1
    Registered User
    Join Date
    10-29-2016
    Location
    london, england
    MS-Off Ver
    2013
    Posts
    7

    Sum by Colour Vba clarification please

    Hi All

    I have found a couple of different Vba methods for count by colour, but would like clarification please:

    1st method:


    Please Login or Register  to view this content.
    The original value in line 3 was "Dim cSum As long" but it would then round up/down the results, which I did not want, so "Dim cSum As single" seems to work...is this correct?
    I can also replace it with "Dim TCell As Range" this also seems to work
    Can someone please explain what the actual differences are?

    2nd Method

    Please Login or Register  to view this content.
    Again could someone please confirm if both methods provide accurate results...?
    Is one method better than the other?
    What are the actual differences?

    Thank you in advance for any insight.

    Kind Regards
    Last edited by ancient333; 10-29-2016 at 02:50 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sum by Colour Vba clarification please

    Welcome to the board.

    Please take a few minutes to read the Forum Rules, and then edit your post to wrap your code with CODE tags.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    10-29-2016
    Location
    london, england
    MS-Off Ver
    2013
    Posts
    7

    Re: Sum by Colour Vba clarification please

    Hi

    Apologies, code tags added.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sum by Colour Vba clarification please

    Please Login or Register  to view this content.
    >> Is one method better than the other?

    Neither method is very good; color is not data to Excel, and making the function volatile doesn't solve the problem that Excel won't recalculate automatically when a cell's color changes.

    In the first function,

    o it makes no sense to use a worksheet function to do simple addition, other than that it accommodates text in the range

    o all number values on a worksheet are Doubles, so conversion to Single adds unnecessary processing and can introduce error

    o ColorIndex returns a Variant/Long; a type conversion to Integer makes no sense

    For both functions, Excel versions 2007+ support RGB color; conversion to ColorIndex is a many-to-one mapping, so you could have cells of various color contributing to the result.

  5. #5
    Registered User
    Join Date
    10-29-2016
    Location
    london, england
    MS-Off Ver
    2013
    Posts
    7

    Re: Sum by Colour Vba clarification please

    If I don't put single in it rounds up/down.
    with the addition of volatile, when the colours are changed and the sheet is saved/closed the figures are updated....is there another way (apart from a calculate all macro)

    Both methods seem to work...what are the actual differences between them

    (To a vba beginner )
    Could you let me know what code you would use?

    Thanks.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sum by Colour Vba clarification please

    The function I posted, a variation on your second function, doesn't round at all.

  7. #7
    Registered User
    Join Date
    10-29-2016
    Location
    london, england
    MS-Off Ver
    2013
    Posts
    7

    Re: Sum by Colour Vba clarification please

    Great...thanks....
    Whats the difference between Dim cSum As Single and Dim TCell As Range ?

    Also could you please explain
    + cell.Value2
    Dim iCol As Long
    Dim cell As Range

    ie what these values actually relate to?

    thanks

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sum by Colour Vba clarification please

    A Dim(ension) statement declares a variable. The type of variable (Long, Single, Range, among many others) controls the type and range of data it can contain.

    You might look on the web for an introductory-level class or book on using VBA with Excel. There's more than be covered in a thread on a board.
    Last edited by shg; 10-30-2016 at 04:31 PM.

  9. #9
    Registered User
    Join Date
    10-29-2016
    Location
    london, england
    MS-Off Ver
    2013
    Posts
    7

    Re: Sum by Colour Vba clarification please

    Thank you.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sum by Colour Vba clarification please

    You're welcome.

+ 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. [SOLVED] Clarification about this site
    By zyzzyva57 in forum Excel General
    Replies: 1
    Last Post: 07-27-2016, 04:57 AM
  2. Importing XML - Clarification
    By cbjroms in forum Excel General
    Replies: 0
    Last Post: 03-11-2008, 12:48 PM
  3. [SOLVED] countif clarification
    By LInda in forum Excel General
    Replies: 9
    Last Post: 03-30-2006, 08:00 PM
  4. [SOLVED] If Then Statement Clarification
    By Joanne in forum Excel General
    Replies: 3
    Last Post: 01-30-2006, 05:30 PM
  5. Beyond VLOOKUP Clarification
    By nebb in forum Excel Formulas & Functions
    Replies: 24
    Last Post: 09-06-2005, 12:05 PM
  6. If function clarification
    By jmcclain in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-23-2005, 04:06 PM
  7. If and Dates Clarification
    By wal50 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-04-2005, 03:06 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