VBA ~ Need help with counting each code based on font color with no strikethroughs.

DoodlesMama

New Member
Joined
Aug 26, 2012
Messages
14
I am creating a report that analyzes pre and post occurances and need to count items based on a 4 character code, that matches a font color but has no strikethroughs. I have a matrix that I reference for the 4 character code, it's description, and last the count...for which I need the formula. The data I need to count is in another worksheet within the workbook.
Below is an example of the matrix and data.
Pre</SPAN>
CODE</SPAN>ITEM</SPAN>QTY</SPAN>
AACB</SPAN>Item 1</SPAN>0</SPAN>
AAOO</SPAN>Item 2</SPAN>0</SPAN>
AAQQ</SPAN>Item 3</SPAN>0</SPAN>
AARR</SPAN>Item 4</SPAN>0</SPAN>

<TBODY>
</TBODY><COLGROUP><COL><COL><COL></COLGROUP>

Post</SPAN>
CODE</SPAN>ITEM</SPAN>QTY</SPAN>
AACB</SPAN>Item 1</SPAN>
AAOO</SPAN>Item 2</SPAN>0</SPAN>
AAQQ</SPAN>Item 3</SPAN>0</SPAN>
AARR</SPAN>Item 4</SPAN>0</SPAN>

<TBODY>
</TBODY><COLGROUP><COL><COL><COL></COLGROUP>


Code</SPAN>
AAZZ</SPAN>
AAZZ</SPAN>
<STRIKE>AAZZ</STRIKE><STRIKE></SPAN></STRIKE>
AAYY</SPAN>
<STRIKE>AAYY</STRIKE><STRIKE></SPAN></STRIKE>
<STRIKE>AAYY</STRIKE><STRIKE></SPAN></STRIKE>
AAXX</SPAN>
AATT</SPAN>
AATT</SPAN>
<STRIKE>AATT</STRIKE><STRIKE></SPAN></STRIKE>
<STRIKE>AATT</STRIKE><STRIKE></SPAN></STRIKE>
<STRIKE>AATT</STRIKE><STRIKE></SPAN></STRIKE>
<STRIKE>AATT</STRIKE><STRIKE></SPAN></STRIKE>
<STRIKE>AATT</STRIKE><STRIKE></SPAN></STRIKE>
<STRIKE>AATT</STRIKE><STRIKE></SPAN></STRIKE>
AATT</SPAN>
<STRIKE>AATT</STRIKE><STRIKE></SPAN></STRIKE>
AATT</SPAN>
AALL</SPAN>
AALL</SPAN>
<STRIKE>AALL</STRIKE><STRIKE></SPAN></STRIKE>
AALL</SPAN>
AALL</SPAN>
AALL</SPAN>
AALL</SPAN>
AALL</SPAN>
AACC</SPAN>
AACC</SPAN>
AACB</SPAN>
AACB</SPAN>
<STRIKE>AACB</STRIKE><STRIKE></SPAN></STRIKE>
AABB</SPAN>
AABB</SPAN>

<TBODY>
</TBODY><COLGROUP><COL></COLGROUP>
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Give this UDF a try.

Rich (BB code):
Function CodeCount(rCode As Range, rTable As Range) As Long


Dim rCell As Range
Dim vColor As Variant
Dim i As Integer


Application.Volatile True
vColor = rCode.Font.ColorIndex
For Each rCell In rTable
    If rCell = rCode Then
        With rCell.Font
            If .ColorIndex = vColor And .Strikethrough = False Then i = i + 1
        End With
    End If
Next rCell
CodeCount = i


End Function

If you are new to UDFs, then follow these simple steps to install the UDF.
1. Open up your workbook
2. Get into VB Editor (Press Alt+F11)
3. Insert a new module (Insert > Module)
4. Copy and Paste in your code (given above)
5. Get out of VBA (Press Alt+Q)
6. Save as a macro-enabled workbook


Here is how you would use the UDF in your spreadsheet. Note! I placed the Code list on Sheet2 cells A2:A42.

Excel 2010
ABC
1Pre
2CODEITEMQTY
3AACBItem 12
4AAOOItem 20
5AAQQItem 30
6AARRItem 42
7Post
8CODEITEMQTY
9AACBItem 12
10AAOOItem 22
11AAQQItem 32
12AARRItem 40

<tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
C3=codecount(A3,Sheet2!$A$2:$A$42)
C4=codecount(A4,Sheet2!$A$2:$A$42)
C5=codecount(A5,Sheet2!$A$2:$A$42)
C6=codecount(A6,Sheet2!$A$2:$A$42)
C9=codecount(A9,Sheet2!$A$2:$A$42)
C10=codecount(A10,Sheet2!$A$2:$A$42)
C11=codecount(A11,Sheet2!$A$2:$A$42)
C12=codecount(A12,Sheet2!$A$2:$A$42)

<tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
Thank you so much for your help Allan! It worked on the first group (red font), but gave the same counts for the second group (blue) font.
I set up the table with the first group for PRE-event counts (red font) like so:

1ABC
2CODEDESCQTY
3AABBITEM 1=codecount(A3,Sheet2!$U$15:$U$2212)
4AACCITEM 2=codecount(A4,Sheet2!$U$15:$U$2212)
5AADDITEM 3=codecount(A5,Sheet2!$U$15:$U$2212)

<TBODY>
</TBODY>

....and the second group for POST-event counts (blue font) like so:

1EFG
2CODEDESCQTY
3AABBITEM 1=codecount(E3,Sheet2!$U$15:$U$2212)
4AACCITEM 2=codecount(E4,Sheet2!$U$15:$U$2212)
5AADDITEM 3=codecount(E5,Sheet2!$U$15:$U$2212)

<TBODY>
</TBODY>


Any additional assistance you can add would be greatly appreciated!

Kind regards,
DoodlesMama
 
Upvote 0
I set up a spreadsheet just as you described and it worked for me. Have you checked to see if Workbook Calculation is set to Automatic? Hit F9 and see if that works.
 
Upvote 0
Sorry for the delay in response, I have been working on a project with a Monday deadline (tomorrow)...I FINALLY figured it out....a portion of this was done in Excel 2007 and my portion is in 2010. Although both font colors look exactly alike they were not which is why the formula did not work. I am very grateful for your assistance. Thank you so much!!
 
Upvote 0

Forum statistics

Threads
1,216,624
Messages
6,131,788
Members
449,672
Latest member
Dervint81

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