Counting Bold words in a cell with a Macro doesn't work right

Mirthern

New Member
Joined
Sep 6, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi! Years ago I asked for a macro for counting Bold words in a text inside a cell for something I was doing, and a user gave me that code below:

VBA Code:
Function nBold(cell As Range) As Long
  If cell.HasFormula Then Exit Function
  Dim tmp, n As Integer, x As Integer
  tmp = Split(Application.Trim(cell.Text))
  For n = 0 To UBound(tmp)
    x = x - cell.Characters(InStr(cell, tmp(n)), 1).Font.Bold
  Next: nBold = x
End Function

And I thought It was right, but recently I started a new project and I tried to use that macro, but... Sometimes the numbers are good sometimes they're not, in one case is because a noun is repeated, in another case the nouns are repeated and the account is right, and I don't know what's the problem...

Someone can help me qith that?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
This seems to do it.

New__Document (1).xlsx
BC
4The quick brown fox jumped over the lazy dog.5
5The quick brown fox jumped over the lazy dog.3
6The quick brown fox jumped over the lazy dog.4
7The quick brown fox jumped over the lazy dog.2
8The brown brown fox jumped over the lazy dog.1
Sheet1
Cell Formulas
RangeFormula
C4:C8C4=nBold(B4)


VBA Code:
Function nBold(cell As Range) As Long
  If cell.HasFormula Then Exit Function
  Dim tmp, n As Integer, x As Integer
  Dim used As String
  tmp = Split(Application.Trim(cell.Text))
  For n = 0 To UBound(tmp)
    x = x - cell.Characters(Len(used) + 1, 1).Font.Bold
    used = used & tmp(n) & " "
  Next: nBold = x
End Function
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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