Count every word which contains a bold character - ready short code, but something is wrong (value error)

makiwara

Board Regular
Joined
Mar 8, 2018
Messages
171
Hi!

I inserted my code and added some comment. I hope that somebody can find the mistake.

Code:
Function CountBold(WorkRng As Range)


    Dim i, xcount As Integer
    For i = 1 To Len(WorkRng)




    If WorkRng.Characters(i, 1).Font.Bold = True Then
    
            If WorkRng.Characters(1, 1).Fold.Bold = True Then
            xcount = 1 ' if first character is bold then we give the value of 1 to xcount ([B]You[/B] ate an [B]apple yesterday.[/B] -->1)

            End If
            
    If WorkRng.Characters(i - 1, 1) = " " Then 'for every other word in cell we count the cases when there is a bold
'letter and a space before it --> ([B]You[/B] ate an [B][COLOR=#ff0000]a[/COLOR]pple [COLOR=#ff0000]y[/COLOR]esterday.[/B] -->1 for "you" and +2 = 3 words
    
        xcount = xcount + 1
    End If
End If


Next i


CountBold = xcount
End Function

Can you spot the mistake? (Value error)

Thank you very much :)
 
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Does this line cause the error?
Code:
If WorkRng.Characters(i - 1, 1) = " "
Since i starts at 1, i-1 is zero, but the Characters object start value must either be omitted or have a minimum value of 1.
You might try replacing that line with this:
Code:
If WorkRng.Characters(1, 1) = " " Then
    'your code to do whatever you want when there's a leading space ...
 
Upvote 0
Also note that every time you find a bold character, you are checking the first characters. So there is a problem with your loop, as well as how you are checking for a space.

This code will do what you want:
Code:
Function CountBold(WorkRng As Range) As Integer

    Dim i As Integer, xcount As Integer
    
    For i = 1 To Len(WorkRng)
        If WorkRng.Characters(i, 1).Font.Bold = True Then
            If i = 1 Then
                xcount = 1
            Else
                If Mid(WorkRng, i - 1, 1) = " " Then xcount = xcount + 1
            End If
        End If
    Next i

    CountBold = xcount

End Function
 
Upvote 0
Thank you very much, works great :-)
Also note that every time you find a bold character, you are checking the first characters. So there is a problem with your loop, as well as how you are checking for a space.

This code will do what you want:
Code:
Function CountBold(WorkRng As Range) As Integer

    Dim i As Integer, xcount As Integer
    
    For i = 1 To Len(WorkRng)
        If WorkRng.Characters(i, 1).Font.Bold = True Then
            If i = 1 Then
                xcount = 1
            Else
                If Mid(WorkRng, i - 1, 1) = " " Then xcount = xcount + 1
            End If
        End If
    Next i

    CountBold = xcount

End Function
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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