Coding for counting bold / strikethrough / underline / formatted cells ?

rjn008

New Member
Joined
May 12, 2023
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Good Morning.

I have the attached and wish to possibly count all or some of the cells that are formatted either by being bold, strikethrough, underlined, italic or coloured text / cell?

Is this possible using similar data to below;

Book2
BC
6Alex Tattersley1Man Utd
7Alex Tattersley2Arsenal
8Ants Farrell (AT)Man Utd
9Ben CummingsBrighton & HA
10Brandon WilkinsonArsenal
11Chris Knutton (AT)Man Utd
12Craig Robbo (G)Brighton & HA
13Daniel CummingsArsenal
14Danny ColbeckArsenal
15Dave CrabtreeNewcastle Utd
16Daz HodgsonMan City
17Dean Barrett1Arsenal
18Dean Barrett2Brighton & HA
19Elliott KenyonBrighton & HA
20Gary CrabtreeBrighton & HA
21Gavin WestNewcastle Utd
22Geordie WatsonMan City
23George Wilkinson (LW)Man Utd
24Glen BroadbentArsenal
25Harvey OakesMan Utd
26Jacob ColcloughMan Utd
27James PinderArsenal
28Jamie BallesterArsenal
29Joe McAdamTottenham
30Joe Slingsby (AT)Brighton & HA
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B2:C30Expression=ROW(B2)=CurrentRowtextNO
Cells with Data Validation
CellAllowCriteria
C28:C30List=$E$76:$E$96
C26List=$E$76:$E$96
C22:C24List=$E$76:$E$96
C6:C8List=$E$76:$E$96
C11:C12List=$E$76:$E$96
C17:C20List=$E$76:$E$96
C14:C15List=$E$76:$E$96
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Please try this code. I put the results in a cell 2 columns to the right. You can change that location by change OutRng.


VBA Code:
Sub CountFormattedCells()
  Dim Cel As Range
  Dim Rng As Range
  Dim OutRng As Range
  Dim Bld As Long
  Dim Ital As Long
  Dim Und As Long
  Dim Strike As Long
  Dim ColTxt As Long
  Dim ColCel As Long
  Dim BG As Boolean
  
  
  Set Rng = Selection
  Set OutRng = Rng.Resize(1, 1).Offset(0, 2)
  
  For Each Cel In Rng
    If Cel.Font.Bold = True Then Bld = Bld + 1
    If Cel.Font.Underline = True Then Und = Und + 1
    If Cel.Font.Strikethrough = True Then Strike = Strike + 1
    If Cel.Font.Italic = True Then Ital = Ital + 1
    With Cel.Font
      If .ColorIndex <> xlAutomatic And .ColorIndex <> 1 Then ColTxt = ColTxt + 1
    End With
    
    With Cel.Interior
      If .Pattern = xlNone And .TintAndShade = 0 And .PatternTintAndShade = 0 Then
        BG = False
      Else
        BG = True
      End If
    End With
    If BG = True Then ColCel = ColCel + 1
    
  Next Cel
  
  OutRng.Offset(0, 0).Value = "Bold:"
  OutRng.Offset(0, 1).Value = Bld
  OutRng.Offset(1, 0).Value = "Italic:"
  OutRng.Offset(1, 1).Value = Ital
  OutRng.Offset(2, 0).Value = "Underline:"
  OutRng.Offset(2, 1).Value = Und
  OutRng.Offset(3, 0).Value = "Strike:"
  OutRng.Offset(3, 1).Value = Strike
  OutRng.Offset(4, 0).Value = "Font Color:"
  OutRng.Offset(4, 1).Value = ColTxt
  OutRng.Offset(5, 0).Value = "BG Color:"
  OutRng.Offset(5, 1).Value = ColCel
  
    
  
End Sub
 
Upvote 0
Thanks for the reply, but I have got this error;

1692177402794.png


I have probably gone a bit extreme on my example above, if it was to work on the below and count the bold cells and then the strikethrough cells;

Book2
CD
4NameWeek 1
5Alex Tattersley1Man Utd
6Alex Tattersley2Arsenal
7Ants Farrell (AT)Man Utd
8Ben CummingsBrighton & HA
9Brandon WilkinsonArsenal
10Chris Knutton (AT)Man Utd
11Craig Robbo (G)Brighton & HA
12Daniel CummingsArsenal
13Danny ColbeckArsenal
14Dave CrabtreeNewcastle Utd
15Daz HodgsonMan City
16Dean Barrett1Arsenal
17Dean Barrett2Brighton & HA
18Elliott KenyonBrighton & HA
19Gary CrabtreeBrighton & HA
20Gavin WestNewcastle Utd
21Geordie WatsonMan City
22George Wilkinson (LW)Man Utd
23Glen BroadbentArsenal
24Harvey OakesMan Utd
25Jacob ColcloughMan Utd
26James PinderArsenal
27Jamie BallesterArsenal
28Joe McAdamTottenham
29Joe Slingsby (AT)Brighton & HA
30
31Bold24
32Strikethrough1
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C4:D29Expression=ROW(C4)=CurrentRowtextNO
Cells with Data Validation
CellAllowCriteria
D27:D29List=$E$76:$E$96
D25List=$E$76:$E$96
D21:D23List=$E$76:$E$96
D5:D7List=$E$76:$E$96
D10:D11List=$E$76:$E$96
D16:D19List=$E$76:$E$96
D13:D14List=$E$76:$E$96


With the results above showing 24 and 1? But as per original example if I had italic or underline or coloured cells I would need the results for this count also? Hope you can help further?
 
Upvote 0
The top code line shouldn't be there and really you want to paste the code in a regular module
I have done that and it just doesn't give me any answers... I am sorry, VBA I aren't the best with
 
Upvote 0
Try...
VBA Code:
Sub countBoldStrk()
    Dim myCell As Range
    Dim myBold As Long, myStrk As Long

    myBold = 0
    myStrk = 0
  
    For Each myCell In Range("D5:D29")
        If myCell.Font.Bold Then myBold = myBold + 1
        If myCell.Font.Strikethrough Then myStrk = myStrk + 1
    Next

    Range("D31") = myBold
    Range("D32") = myStrk

End Sub
 
Upvote 0
Try...
VBA Code:
Sub countBoldStrk()
    Dim myCell As Range
    Dim myBold As Long, myStrk As Long

    myBold = 0
    myStrk = 0
 
    For Each myCell In Range("D5:D29")
        If myCell.Font.Bold Then myBold = myBold + 1
        If myCell.Font.Strikethrough Then myStrk = myStrk + 1
    Next

    Range("D31") = myBold
    Range("D32") = myStrk

End Sub
How do I apply this? I know to press Alt F11 to open VBA Code Object, I have pasted as below, but when I go back to my sheet, I dont see anything?

1692189566382.png
 
Upvote 0
I have done that and it just doesn't give me any answers... I am sorry, VBA I aren't the best with
The code I gave you needs to be put in a Standard Module, not a sheet module. Sorry for the confusion.

1692194860382.png
 
Upvote 0
How do I apply this? I know to press Alt F11 to open VBA Code Object, I have pasted as below, but when I go back to my sheet, I dont see anything?
Alt + F8, select the macro name (if more than one exists) and click Run or assign the code to a button or shape
 
Upvote 0
The code I gave you needs to be put in a Standard Module, not a sheet module. Sorry for the confusion.

View attachment 97254
@Jeffrey Mahoney I have managed to work this out now how to run on both the coding above... but when i run the original one you sent it opens VBA nd highlights 'End Sub' as below, do you know why?
1692195569309.png

Alt + F8, select the macro name (if more than one exists) and click Run or assign the code to a button or shape
Hi @MARK858 I have got a button now and it is working as I like, could you advise the logic to be added if I was to format 'Italic' and 'Underline'?
 
Upvote 0

Forum statistics

Threads
1,215,143
Messages
6,123,277
Members
449,093
Latest member
Vincent Khandagale

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