How to color code a cell when using the & function in excel

Mark McInerney

Active Member
Joined
Apr 4, 2012
Messages
283
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have data in Cell A1 and A2, 4 and 8 respectively.

In cell A3 I need to combine both values - I am using =(A1&A2). I want both values to be formatted as numbers and I want the second value (i.e. A2 to have red text - appear in red).

Any help appreciated.

Thanks - Mark.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Don't think it can be done with formulas or conditional formatting and also I don't think you can change the font colour with a Function so you'd probably have to look at using something like a worksheet_change event to kick off your vba. Also not sure of why you insist on the values to be formatted as numbers as excel treats numbers enclosed in brackets as a negative number, is that the reason for insisting on numbers? Excel will still allow you to treat the values as numbers. Maybe this will be of use or not but at least it'll get the thread started, the contents in A3 can still be treated as a number and still be added etc +A3 and you can always change the alignment to left if needed

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo xit:
Application.ScreenUpdating = False
Application.Calculation = xlCalculateManual
Dim strlen1 As Integer, strlen2 As Integer


If Not Application.Intersect(Target, Range("A1:A2")) Is Nothing Then
Range("A3").Font.ColorIndex = 1 ' reset colour
 
Range("A3").Value = "'(" & Range("A1").Text & Range("A2").Text & ")"


strlen1 = Len(Range("A1").Value)
strlen2 = Len(Range("A2").Value)


Range("A3").Characters(Start:=strlen1 + 2, Length:=strlen2).Font.ColorIndex = 3
End If
  
xit:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
Last edited:
Upvote 0
MrTeeny is correct, you cannot format part of a cell with anything other than VBA (or manually)
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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