0

I wanted to form a text in a new cell based on input from other cells and in new cell I wanted to highlight selected text with different color, following is my sample code

Sub Macro1()

   While ActiveCell.Offset(0, -1) <> ""       
     ActiveCell.Offset(0, 0).FormulaR1C1 = _
      "New Text" & ActiveCell.Offset(0, -1) & ":" & ActiveCell.Offset(0, -2)

     ActiveCell.Offset(1, 0).Select
   Loop

End Sub

Here I want to define color to "New Text", I have seen how to change for entire cell, but not for a selected text. how to do this?

1
  • 3
    See this SO post. Commented Jul 6, 2013 at 10:38

2 Answers 2

1

Here is an example of changing specific characters within a string to another colour (red in this case):

Sub ChgColour()

    vStart = 12
    vLength = 3
    With ActiveCell.Characters(Start:=vStart, Length:=vLength).Font
       .Color = -16776961
    End With

End Sub

This changes the colour of three characters starting from the 12th.

One tip that I have found very useful for getting VBA syntax when I'm not sure is to record a macro whilst doing the steps manually, you can then see the code that was used. Sometimes it generates a little more code than needed, like setting all of the font properties rather than just the one you want, but it is often easy to work out which part you need and replace the absolute cell / character references with relative ones.

Sign up to request clarification or add additional context in comments.

Comments

-1

Use this:

Do While ActiveCell.Offset(0, -1) <> ""
 ActiveCell.Offset(0, 0).FormulaR1C1 = _
  "New Text " & ActiveCell.Offset(0, -1) & ":" & ActiveCell.Offset(0, -2)

 With ActiveCell.Characters(Start:=0, Length:=8).Font
    .Color = -16776961
 End With

 ActiveCell.Offset(1, 0).Select

Loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.