Format A String

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this line of code which populates a cell with a text string

Code:
.Range("B" & t) = ptyp & pt

Is it possible, and if so how, to format the result so that ptyp is bold while pt remains normal? eg ptype = Change and pt >8:00P so that the finished result in B# is 'Change >8:00P'
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I have this line of code which populates a cell with a text string

Code:
.Range("B" & t) = ptyp & pt

Is it possible, and if so how, to format the result so that ptyp is bold while pt remains normal? eg ptype = Change and pt >8:00P so that the finished result in B# is 'Change >8:00P'
Try this as the next code line...
Code:
.Range("B" & t).Characters(1, Len(ptyp)).Font.Bold = True
Note: I used ptyp because you repeated that spelling a couple of times, but in your description where you said you assigned "Change" to it, you spelled it ptype (with an 'e' at the end), so you will need to verify the spelling of the variable name.
 
Upvote 0
I hope this is what you are looking for.

Code:
.Range("B" & t) = ptyp & pt
.Range("B" & t).Characters(1, Len(ptyp)).Font.Bold = True

ZAX
 
Upvote 0
Sorry For Posting After You Mister But The Forum Didn't Show Your Post Right Away. :oops:
 
Upvote 0
Thanks folks for your help ... works beautifully. (and yes Rick ... I missed that one bad reference in my message ... you were correct.)

Anyway, could this code have any lingering effects?

Here is the code that that line is in:

Code:
    For t = 13 To 22     
                   Dim gt As String
                    Dim pt As String
                    Dim ptyp As String
                
                    gt = Application.VLookup(.Range("A" & t), rcore, 23, False)
                    pt = Application.VLookup(.Range("A" & t), rcore, 30, False)
                    ptyp = Application.VLookup(.Range("A" & t), rcore, 26, False)
                
                    If .Range("H" & t) = pristaff Then 'groom time
                        .Range("B" & t) = gt
                    ElseIf .Range("I" & t) = pristaff Then
                        If ptyp = "Initial" Then
                            .Range("B" & t) = pt
                        Else
                            .Range("B" & t) = ptyp & pt
                            .Range("B" & t).Characters(1, Len(ptyp)).Font.Bold = True
                        End If
                    End If
                    .Range("B" & t).Font.Size = 6
            next t

In my testing, cells (in columb B) after the one in which the bold format has been applied are also bolded. So values where ptyp = "initial" are bolded when they shouldn't be when they follow a B# in which there has been bolding applied.
 
Upvote 0
Rich (BB code):
    For t = 13 To 22     
                   Dim gt As String
                    Dim pt As String
                    Dim ptyp As String
                
                    gt = Application.VLookup(.Range("A" & t), rcore, 23, False)
                    pt = Application.VLookup(.Range("A" & t), rcore, 30, False)
                    ptyp = Application.VLookup(.Range("A" & t), rcore, 26, False)

                    Range("B" & t).Characters.Font.Bold = False

                    If .Range("H" & t) = pristaff Then 'groom time
                        .Range("B" & t) = gt
                    ElseIf .Range("I" & t) = pristaff Then
                        If ptyp = "Initial" Then
                            .Range("B" & t) = pt
                        Else
                            .Range("B" & t) = ptyp & pt
                            .Range("B" & t).Characters(1, Len(ptyp)).Font.Bold = True
                        End If
                    End If
                    .Range("B" & t).Font.Size = 6
            next t

In my testing, cells (in columb B) after the one in which the bold format has been applied are also bolded. So values where ptyp = "initial" are bolded when they shouldn't be when they follow a B# in which there has been bolding applied.

Try adding the line of code I show in red above and see if that solves your problem.
 
Upvote 0
Seems to have done the trick Rick. Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,800
Messages
6,121,641
Members
449,044
Latest member
hherna01

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