Bold part of a string

B

Bill D.

I have a string in a cell and I want the first 14 characters to be Bold
and the rest to be the default format. Example:

Customer Name: ABC Company Inc.

Customer Name: should be Bold

The entry is via a UserForm

Private Sub TextBox2_Change()
Range("C6").Font.Bold = True
Range("C6") = "Customer Name: " & TextBox2.Text
End Sub

As it is now, all of the entry is Bold

I believe I need to apply the Left function here.

Bill

There are 10 types of people; those that understand binary and those
that don't.

Remove NOTME to reply direct
 
V

Vasant Nanavati

Private Sub TextBox2_Change()
Range("C6") = "Customer Name: " & TextBox2.Text
Range("C6").Characters(1, 14).Font.Bold = True
End Sub
 
J

JE McGimpsey

One way:

Const sPrefix As String = "Customer Name: "
With Range("C6")
.Value = sPrefix & TextBox2.Text
.Characters(Len(sPrefix) + 1).Font.Bold = True
End With
 
T

Tom Ogilvy

Private Sub Textbox2_Change()
Range("C6").Value = "Customer Name: " & TextBox2.Text
Range("C6").Font.Bold = False
Range("C6").Characters(1, 14).Font.Bold = True
End Sub

Worked for me.
 
T

Tom Ogilvy

Both JE's and Vasants solutions bolded the whole string but could be fixed
by adding in
Range("C6").Font.Bold = False
before the command containing the characters command.

as I did in mine.

Tested all three in xl2000
 
J

JE McGimpsey

Oops, wrong part of string. Change

.Characters(Len(sPrefix) + 1).Font.Bold = True

to

.Characters(1,Len(sPrefix)).Font.Bold = True
 
J

JE McGimpsey

I misread the OP, so mine bolded only the second part (unless you'd
already bolded the first part).
 
T

Tom Ogilvy

Private Sub TextBox2_Change()
Const sPrefix As String = "Customer Name: "
With Range("C6")
.Value = sPrefix & TextBox2.Text
.Characters(1, Len(sPrefix)).Font.Bold = True

End With
End Sub

Bolded the whole cell. I manually unbolded it before showing the userform.

Private Sub TextBox2_Change()
Const sPrefix As String = "Customer Name: "
With Range("C6")
.Value = sPrefix & TextBox2.Text
.Font.Bold = False
.Characters(1, Len(sPrefix)).Font.Bold = True

End With
End Sub

worked for me xl2000

Your code should work, but being in the change event, I believe the
formatting somehow gets transferred to being the default.

Perhaps it works on the MAC. (both were tested in Excel 2000, SR1, under
Windows 2K)
 
J

JE McGimpsey

Ah, my fault - I didn't put it in the change event. Works the same in
MacXL.

Bug report's in for the next version.
 
B

Bill D.

Thanks all for your help.

Bill

There are 10 types of people; those that understand binary and those
that don't.

Remove NOTME to reply direct
 

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