determining bold font in a string

  • Thread starter william kossack
  • Start date
W

william kossack

I have a spreadsheet where in order to code the importance of one word
over another in a string words were entered in a bold in the string.

Is there a function I can use to identify bold text in a string so it
can be separated into a new column?
 
D

Dave Peterson

You could use a userdefined function to extract the bold characters:

Option Explicit
Function GetBoldChars(myCell As Range) As String

Application.Volatile True

Dim iCtr As Long
Dim OutStr As String

Set myCell = myCell.Cells(1) 'one cell at a time

OutStr = ""
If myCell.Font.Bold = True Then
OutStr = myCell.Value
Else
If myCell.Font.Bold = False Then
OutStr = ""
Else
For iCtr = 1 To Len(myCell.Value)
If myCell.Characters(Start:=iCtr, Length:=1).Font.Bold _
= True Then
OutStr = OutStr & Mid(myCell.Value, iCtr, 1)
End If
Next iCtr
End If
End If

GetBoldChars = OutStr

End Function

Be aware that if you change the formatting of the cell, then this formula cell
won't change until your workbook calculates.

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Short course:

Open your workbook.
Hit alt-f11 to get to the VBE (where macros/UDF's live)
hit ctrl-R to view the project explorer
Find your workbook.
should look like: VBAProject (yourfilename.xls)

right click on the project name
Insert, then Module
You should see the code window pop up on the right hand side

Paste the code in there.

Now go back to excel.
Into a test cell and type:
=GetBoldChars(a1)
 
D

Don Guillett

a sub for the active cell
Sub findboldincell()
Set ac = Selection
For i = 1 To Len(ac)
If ac.Characters(i, 1).Font.Bold Then
ms = ms & Mid(ac, i, 1)
End If
Next i
ac.Offset(, 1) = ms
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top