Format part of text string, not entire cell (VBA)

Craig__

Board Regular
Joined
Feb 16, 2010
Messages
66
Below is a macro with an InputBox for users to add a reference number beneath existing names in a column. For example, a user might select a cell with "John Smith & Co." in it, and the result might be:

John Smith & Co
Code Number:
hwu 453290

Is there a way to change the inputted text (hwu 453290) on line 3 to blue, and also change the inputted text to uppercase if not already in uppercase.

Additional notes:
Line 1 and 2 must remain in black text.
I need all 3 lines to be in the same cell rather than on 3 separate rows.
The reference numbers are not of a constant length, they can be anywhere between 7-14 characters.

Thanks guys.


Sub Format_Text_From_InputBox()
Dim strName As String
strName = InputBox(Prompt:="Please type your reference number" _
& vbCr & "in the box below, then click OK.", _
Title:="", Default:="")
If strName = " " Or _
strName = vbNullString Then
Exit Sub
Else
ActiveCell.Value = ActiveCell.Value & Chr(10) _
& "Code Number:" & Chr(10) & strName
End If
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Give this modification to your code a try...

Code:
Sub Format_Text_From_InputBox()
  Dim strName As String, LastLineFeed As Long
  strName = InputBox(Prompt:="Please type your reference number" & vbLf & "in the box below, then click OK.", Title:="", Default:="")
  If Trim(strName) = "" Then Exit Sub
  ActiveCell.Value = ActiveCell.Value & vbLf & "Code Number:" & vbLf & strName
  LastLineFeed = InStrRev(ActiveCell.Value, vbLf)
  ActiveCell.Characters(LastLineFeed + 1).Font.ColorIndex = 5
End Sub
 
Upvote 0
Many thanks Vog and Rick for your help. Rick this is just what I needed, it works great. Cheers.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

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