PDA

View Full Version : Solved: Bold part of text inserted into cell?



clhare
02-18-2008, 06:43 AM
I need to insert text into a cell, via macro, and bold part of the text. How would I do that?

Example: "Regular text and bolded text"

Bob Phillips
02-18-2008, 07:11 AM
With Range("A1")
.Value = "Regular text and bolded text"
.Characters(19, 11).Font.Bold = True
End With

clhare
02-18-2008, 08:01 AM
But what if some of the text is always the same and some (the part I want bolded) is always different? How do I bold it then?

Norie
02-18-2008, 08:04 AM
Cheryl

How would you, we or VBA determine what's to be bold and what's not?:)

clhare
02-18-2008, 08:39 AM
Originally, the macro was in a Word template. In the Word table cell, the macro inserted the static part of the variable text, toggled bold on, then inserted the rest of the varible text. I've never really worked with Excel macros before, so I have no idea how to do the same thing in an Excel macro.

Bob Phillips
02-18-2008, 08:58 AM
With Range("A1")
.Value = "Regular text and bolded text"
.Characters(len_of_always_the_same_bit + 1, Len(.Value) - len_of_always_the_same_bit).Font.Bold = True
End With

clhare
02-18-2008, 09:26 AM
Ok, Here's the code you gave me for adding text to column B of the active row. How do I modify it to apply bold to "bolded text"? Since this doesn't use a range, I'm not sure how to alter it.

ActiveSheet.Cells(ActiveCell.Row, "B").Value = "Regular text and bolded text"

Bob Phillips
02-18-2008, 09:45 AM
It does use a range, Cells is a range property, so it works the ame way



Dim mpStart As Long

With ActiveSheet.Cells(ActiveCell.Row, "B")

.Value = "Regular text and bolded text"
mpStart = InStr(.Value, "bolded text")
If mpStart > 0 Then

.Characters(mpStart, Len(.Value) - mpStart).Font.Bold = True
End If
End With

clhare
02-18-2008, 09:49 AM
The only thing the above code didn't do was bold the last letter in "bolded text".

Bob Phillips
02-18-2008, 09:53 AM
It is pretty simple to work out the fix



With ActiveSheet.Cells(ActiveCell.Row, "B")

.Value = "Regular text and bolded text"
mpStart = InStr(.Value, "bolded text")
If mpStart > 0 Then

.Characters(mpStart, Len(.Value) - mpStart + 1).Font.Bold = True
End If
End With