+ Reply to Thread
Results 1 to 7 of 7

Count Cells in Date Range and Cell Color (using VBA)

Hybrid View

newoak14 Count Cells in Date Range and... 09-27-2021, 04:43 PM
alansidman Re: Count Cells in Date Range... 09-27-2021, 04:53 PM
newoak14 Re: Count Cells in Date Range... 09-27-2021, 05:08 PM
alansidman Re: Count Cells in Date Range... 09-27-2021, 05:31 PM
newoak14 Re: Count Cells in Date Range... 09-27-2021, 09:00 PM
alansidman Re: Count Cells in Date Range... 09-27-2021, 09:05 PM
newoak14 Re: Count Cells in Date Range... 09-28-2021, 09:37 AM
  1. #1
    Registered User
    Join Date
    09-27-2021
    Location
    USA
    MS-Off Ver
    Office 365, Excel
    Posts
    4

    Count Cells in Date Range and Cell Color (using VBA)

    Hi all - First time posting here. Thanks in advance for the help. I'm familiar with basic Excel functions and formulas. Running Office 365. As part of a recent project, I have a list of dates. I need to count the number of cells within a date range AND if they colored red or green. The team uses red/green to flag a specific aspect to minimize data tracking.

    I can easily count the cells in the date range using the COUNTIFS function (e.g. =COUNTIFS(B14:B17,">=8/1/2021",B14:B17,"<=8/31/2021"). Using a macro found online, I am able to successfully count the number of cells of each color by creating a custom function called "colorfunction" (e.g. =colorfunction(F2,B14:B17,FALSE, where F2 is the reference cell color, the TRUE/FALSE statement is for summing and has no relevance to my use).

    Unfortunately, I'm unable to nest the "colorfunction" function within the COUNTIFS function as a third criteria to accomplish my final goal (e.g. =COUNTIFS(J1:J5,">=8/1/2021",J1:J5,"=8/31/2021",J1:J5,"=colorfunction(F2,J1:J5,FALSE)"). I tried variations within the COUNTIFS equation, but this is where my lack of VBA experience shines.

    Any support is greatly appreciated!
    Attached Files Attached Files
    Last edited by newoak14; 09-27-2021 at 05:07 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,755

    Re: Count Cells in Date Range and Cell Color (using VBA)

    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    09-27-2021
    Location
    USA
    MS-Off Ver
    Office 365, Excel
    Posts
    4

    Re: Count Cells in Date Range and Cell Color (using VBA)

    Hi Alan - My apologies. File uploaded as an attachment to the original post.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,755

    Re: Count Cells in Date Range and Cell Color (using VBA)

    Try this VBA solution

    Option Explicit
    
    Sub CntGrn()
        Dim i As Long, lr As Long
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Dim Mth As Integer
        Mth = InputBox("What month to find? Use Month Number, ie. 8 for August")
        Dim x As Long
        x = 0
        For i = 9 To lr
            If Month(Range("A" & i)) = Mth And Range("A" & i).Interior.ColorIndex = 43 Then
                x = x + 1
            End If
        Next i
        Range("D14") = x
    End Sub

  5. #5
    Registered User
    Join Date
    09-27-2021
    Location
    USA
    MS-Off Ver
    Office 365, Excel
    Posts
    4

    Re: Count Cells in Date Range and Cell Color (using VBA)

    Thanks for the quick solution, Alan. Can you help me better understand how I use that within the existing spreadsheet? Is this in place of my existing macro or in addition to? Is it used within the COUNTIFS function?

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,755

    Re: Count Cells in Date Range and Cell Color (using VBA)

    It is instead of your code. It does not use the countifs function.

    It loops through column A. It asks that if it falls within the month asked in the input box and the color of the cell is green then it accumulates that count and places it in cell you have indicated for the result.

    How to install your new code
    • Copy the Excel VBA code
    • Select the workbook in which you want to store the Excel VBA code
    • Press Alt+F11 to open the Visual Basic Editor
    • Choose Insert > Module
    • Edit > Paste the macro into the module that appeared
    • Close the VBEditor
    • Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)


    To run the Excel VBA code:
    • Press Alt-F8 to open the macro list
    • Select a macro in the list
    • Click the Run button

  7. #7
    Registered User
    Join Date
    09-27-2021
    Location
    USA
    MS-Off Ver
    Office 365, Excel
    Posts
    4

    Re: Count Cells in Date Range and Cell Color (using VBA)

    Thanks, Alan.

    I got the macro working. Please see the attached spreadsheet update explaining the expanded use. This will be used for a monthly report of multiple milestones. Please let me know if you have additional questions.
    Attached Files Attached Files

+ 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] Count Cells in a range with a Certain Color
    By AllisterB in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-06-2021, 05:29 AM
  2. Count cells in a Date Range that contain case sensitive text in another cell on same row
    By SpaceCityCowboy88 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-21-2019, 04:41 PM
  3. Replies: 4
    Last Post: 09-12-2013, 11:32 PM
  4. Count cells in a range that have same bacgoround color
    By tavaritz in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 04-27-2013, 09:35 AM
  5. Count Cells in a Row based on color and date range
    By indnracn2 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-14-2009, 11:12 PM
  6. [SOLVED] Is there a way to count cells in a range based on fill color?
    By eehinmd in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-16-2006, 01:15 PM
  7. Replies: 2
    Last Post: 01-19-2005, 04:06 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