Statistics on the sum and quantity of irregular data

Source: Internet
Author: User
I recently went to an enterprise training excel. Some students asked the following question: What should I do if I want to calculate some irregular cell values in a data area?

The solution is as follows:

1. First, set the font color of cells to be counted to a color that is not used throughout the area;

2. Call the following functions for statistics;

3. parameter description: Rng: the entire area to be counted; Rng2: the cell with the same font color to be counted;

4. Small functions compiled using vba are as follows:

Function SumByColor (Rng As Range, Rng2 As Range) As Double

Dim R As Range

Dim Total As Double

For Each R In Rng. Cells

If R. Font. Color = Rng2.Font. Color Then

Total = Total + R. Value

End If

Next R

SumByColor = Total

End Function

Function CountByColor (Rng As Range, Rng2 As Range) As Long

Dim R As Range

Dim Count As Long

For Each R In Rng. Cells

If R. Font. Color = Rng2.Font. Color Then

Count = Count + 1

End If

Next R

CountByColor = Count

End Function

5. Open the excel VB coding environment;

6. Enter the vbprogramming environment and insert the module;

7. copy the above program to the following position:

 

8. Check the effect according to the following instance verification!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.