Format A String

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,526
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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,154
Office Version
  1. 2019
  2. 2010
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'
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

ZAX

Well-known Member
Joined
Jul 5, 2012
Messages
715
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

ZAX

Well-known Member
Joined
Jul 5, 2012
Messages
715
Sorry For Posting After You Mister But The Forum Didn't Show Your Post Right Away. :oops:
 
Upvote 0

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,526
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
ADVERTISEMENT
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

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,154
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,526
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Seems to have done the trick Rick. Thank you.
 
Upvote 0

Forum statistics

Threads
1,195,619
Messages
6,010,736
Members
441,567
Latest member
Flitbee

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
Top