Find and extract/copy bolded portions of a string in a cell

hfearing

New Member
Joined
Jun 28, 2015
Messages
3
In an Excel spreadsheet, I have cells that contain text in a combination of regular and bold font. I would like to have a function that allows me to look at a cell and copy out the portion of the string that is bold.

For example, if cell H5 contained:

F Nx A

I would like to have a user-defined function GetBold(H5) that would look in cell H5 and return "Nx".

Thanks for your help.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
In an Excel spreadsheet, I have cells that contain text in a combination of regular and bold font. I would like to have a function that allows me to look at a cell and copy out the portion of the string that is bold.

For example, if cell H5 contained:

F Nx A

I would like to have a user-defined function GetBold(H5) that would look in cell H5 and return "Nx".

Thanks for your help.
If you want the output in bold font, format the cells you place this formula in as bold.
Code:
Function GetBold(R As Range)
Dim i As Long, S As String
For i = 1 To R.Characters.Count
    If R.Characters(i, 1).Font.Bold = True Then S = S & Mid(R.Value, i, 1)
Next i
If S = "" Then
    GetBold = ""
Else
    GetBold = S
End If
End Function
 
Upvote 0
A slightly different approach with a slightly different result if there are non-contiguous bolded text in the cell. For example, if this is in the cell...

If AAA and BBB Then

JoeMo's code returns AAABBB whereas my code below will return AAA BBB (with a single space separating each non-contiguous bolded text. If you only have a single word bolded, then both our codes return the same thing.
Code:
Function GetBold(R As Range) As String
  Dim X As Long
  GetBold = R(1).Value
  For X = 1 To Len(GetBold)
    If Not R(1).Characters(X, 1).Font.Bold Then Mid(GetBold, X) = " "
  Next
  GetBold = Application.Trim(GetBold)
End Function
 
Upvote 0

Forum statistics

Threads
1,214,394
Messages
6,119,262
Members
448,880
Latest member
aveternik

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