How to make selected partial text in red in the same cell using vba

shoun2502

New Member
Joined
Nov 14, 2018
Messages
43
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Team,
Can you please help me in writing vba code to make partial selected text as red in the same cell. I am not looking for a text box search string option
VBA Code:
Selection.Font.Color = vbRed

This code works good in making the complete text as red in a cell but not partial .

1687632663263.png


1. I need to select text
2. Run Vba code
3. convert font color of the selected text as red.

Any help from the team ll be greatly appreciated.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I don't think you can execute VBA code when you are in cell edit mode which you are when you select partial text in a cell.

This code will color red the 5th to the 8th character in a cell. Selection refers to the selected cell and not the selected text.

VBA Code:
Selection.Characters(5, 4).Font.Color = vbRed

Characters Method

Returns a Characters object that represents a range of characters within a shape’s text frame. You can use the Characters object to add and format characters within the text frame.

expression.Characters(Start, Length)

expression Required. An expression that returns a Characters object in the specified text frame.

Start Optional Variant. The first character to be returned. If this argument is either set to 1 or omitted, the Characters method returns a range of characters starting with the first character.

Length Optional Variant. The number of characters to be returned. If this argument is omitted, the Characters method returns the remainder of the string (everything after the character that was set as the Start argument).

Example


This example formats as bold the third character in the first shape’s text frame on the active worksheet.

With ActiveSheet.Shapes(1).TextFrame
.Characters.Text = "abcdefg"
.Characters(3, 1).Font.Bold = True
End With
 
Upvote 0
@AlphaFrog is correct, once your cursor is in a cell, you are in edit mode... you cannot run VBA code while Excel is in edit mode. But I am wondering why you want a macro to do this? Once you select the text in the cell, you can set its color from the Ribbon directly by clicking the "down arrow" next to the icon labeled "A" on the Home tab in the Font panel (although it will probably be defaulted to red already).... that has to be easier than pressing ALT+F8, selecting the macro and clicking the Run button.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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