Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a subset of text in Excel Cell Using Formula

I Have built a string using a formula in excel. as an example

Cell C3 contains text "Languages"
Cell C4 = "English, Spanish,German, French"
My Forumla = C3 & ":" & CHAR(10) & C4

The Desired text would be:

Languages:
English, Spanish, German, French

(where the bold text would actually be some color like red)

Is there a way to do this in Excel (change partial text formatting) .

I Have tried a formula... (Not working)

Function formatText(InText As Range)

'Set font color
  InText.Characters(1.5).Font.Color = Red
   'InText.Characters((InStr(1, ":", InText) + 1), (Len(InText) - InStr(1, ":", InText))).Font.ColorIndex = 3
End Function
like image 457
Hightower Avatar asked Feb 28 '13 07:02

Hightower


People also ask

How do you subset text in Excel?

Although there is no such thing as Substring function in Excel, there exist three Text functions (LEFT, RIGHT, and MID) to extract a substring of a given length. Also, there are FIND and SEARCH functions to get a substring before or after a specific character.

How do you subset data in Excel?

The “AutoFilter” or “Sort and Filter” function in Excel allows you to create views of data matching your specified criteria. The subset will show only the rows you wish to see. You can perform several functions on the filtered subset of the data without needing to copy and paste the data into a new worksheet.

How do you write a formula if a cell contains certain text?

If cell contains specific text, then return a value Select the output cell, and use the following formula: =IF(cell="text", value_to_return, ""). For our example, the cell we want to check is A2, the text we're looking for is “example”, and the return value will be Yes.

How do I edit text in a cell with a formula?

Click the cell that contains the data that you want to edit, and then click anywhere in the formula bar. This starts Edit mode and positions the cursor in the formula bar at the location that you clicked. Click the cell that contains the data that you want to edit, and then press F2.


2 Answers

Your posted function with work if and only if

  • It is called from a Sub (ie, as other have mentioned, not as a UDF)

And

  • the value(s) contained in range InText are string constants. (This is the main point of my answer)

It will not work for any cells in range InText containing a formula. AFAIK you cannot format part of a string returned by a formula.

BTW I would love to be proved wrong on this!

like image 197
chris neilsen Avatar answered Sep 28 '22 04:09

chris neilsen


Regarding Hightower's question, "how would you cast a formula output to a string so that you can apply the text formatting?"

To "cast" the output of a formula so that you can apply text formatting, you must write the value returned by the formula into the spreadsheet, then apply the formatting to the value you wrote. You could either write the value into the cell containing the formula (which will erase the formula), or you could write the value into a different place in the spreadsheet (which will preserve the formula but then you'll be seeing double).

Sub Cell_Format(InText as Range)
  InText.formula = cstr(InText.value)  ' converts result of formula into a string literal   
    'or: InText.offset(0,1).formula = cstr(InText.value) -- writes the value in the cell next to InText
  InText.characters(1, 5).font.color = vbRed
End Sub

Then Cell_Format range("$A$1") will replace the formula in cell $A$1 with a string constant and change the color of the first five characters to red.

If you want to do this for a range larger than one cell, add this code to the above:

Sub Range_Format(InText as Range)
  For each c in InText
    Cell_Format(c)
  Next
End Sub
like image 21
tpkaplan Avatar answered Sep 28 '22 06:09

tpkaplan