Formatting Issues

Change Text Color Before a Hyphen in Excel Cells

Huma Ashraf ChHuma Ashraf Ch Oct 10, 2026 871 views

Question details

The user needs to apply a specific font color (red) only to the characters preceding a hyphen within a cell, while keeping the rest of the text black.

How to Change Text Color Before a Hyphen in Excel Cells
Product
Microsoft Excel
Device & OS
not provided
Scenario
Formatting text data in a column where a hyphen separates a prefix from the remaining content.
Observed behavior
Excel does not natively support applying multiple font colors within a single cell using standard formulas or conditional formatting.
Before you start

Before proceeding, ensure that your data is stored as plain text, because partial cell formatting cannot be applied to cells containing active formulas.

Solution 1Recommended

Use a VBA Macro for Bulk Partial Formatting

The most efficient method for applying partial text colors across a large dataset or an entire column.

Because Excel formulas and conditional formatting rules apply to the entire cell, they cannot change the color of just a portion of the text. To automate changing the color before a hyphen across multiple cells, you must use a VBA macro.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications window.

2
Insert a New Module

Click on 'Insert' in the top menu and select 'Module' to create a blank script window.

3
Enter the VBA Code

Paste a VBA script that loops through your selected cells, uses the InStr function to find the position of the hyphen, and applies the color index (e.g., Red) to the Characters object before that position.

4
Run the Macro

Close the VBA editor. Highlight the cells you want to format in your worksheet, press Alt + F8, select your macro, and click 'Run'.

Use a VBA Macro for Bulk Partial Formatting
Macro Security: Ensure that macros are enabled in your Excel Trust Center settings before attempting to run the VBA code.
Free Microsoft Office alternative

Try WPS Office for Your Spreadsheet Needs

If you are looking for a powerful, lightweight, and free alternative to Microsoft Office, WPS Office offers excellent compatibility with Excel formats and handles complex data processing smoothly.

Free and lightweight Office suiteFully compatible with Microsoft Excel formats (.xlsx, .xls) and CSV filesIntuitive user interface for advanced data formattingSeamless migration with familiar tools and layout
microsoft office alternative - wps office

Frequently Asked Questions

Can I use Conditional Formatting to change part of the text color?

No, Excel's Conditional Formatting applies styles to the entire cell. To format only a portion of the text within a cell, you must use manual formatting or a VBA macro.

Why does my partial text formatting disappear?

If the cell contains a formula, the formatting will reset to the default style whenever the formula recalculates. You must convert the formula to plain text (Copy > Paste Special > Values) before applying partial font colors.

Can I color text after the hyphen instead?

Yes. If formatting manually, simply highlight the text after the hyphen. If using VBA, modify the script to target the characters starting from the hyphen's position to the end of the text string.

Is it possible to achieve this in Google Sheets?

Like Excel, Google Sheets does not support partial cell formatting via standard formulas. You would need to format the text manually or use Google Apps Script for automation.