Replaces existing text with new text via an array of SUBSTITUTE arguments
=IF(
COLUMNS(search_array)>2,
SUBSTITUTE(
SUBSTITUTEBYARRAY(
text_to_search,
ARRAY_CONSTRAIN(search_array,1,COLUMNS(search_array)-2)
),
INDEX(search_array,1,COLUMNS(search_array)-1),
INDEX(search_array,1,COLUMNS(search_array))
),
SUBSTITUTE(
text_to_search,
INDEX(search_array,1,1),
INDEX(search_array,1,2)
)
)Source: Benny Blue
This function provides a cleaner-looking alternative to multiple nested uses of SUBSTITUTE by taking only two arguments — the text to search within, and an ordered array of alternating searches and replacements. Each pair in the array is applied in order, left to right, so later replacements operate on the result of earlier ones. For example, if A2 contains "OLD MAN" then =SUBSTITUTEBYARRAY(A2,{"OLD","Elderly","MAN","Male","YOUNG","Youthful","WOMAN","Female"}) returns "Elderly Male". Searches that aren't found in the text are simply skipped, so a single shared array of replacements can be reused across a whole column.
Use the inputs below to create SUBSTITUTEBYARRAY as a reusable custom function in Google Sheets.
Learn how to add custom functions to Google SheetsSUBSTITUTEBYARRAY
Replaces existing text with new text via an array of SUBSTITUTE arguments
=IF( COLUMNS(search_array)>2, SUBSTITUTE( SUBSTITUTEBYARRAY( text_to_search, ARRAY_CONSTRAIN(search_array,1,COLUMNS(search_array)-2) ), INDEX(search_array,1,COLUMNS(search_array)-1), INDEX(search_array,1,COLUMNS(search_array)) ), SUBSTITUTE( text_to_search, INDEX(search_array,1,1), INDEX(search_array,1,2) ) )
text_to_search
search_array
The text within which to search and replace
"OLD MAN"
An array of strings alternating between those to search for within text_to_search and those which will replace the strings preceding them
{"OLD","Elderly","MAN","Male","YOUNG","Youthful","WOMAN","Female"}
Other functions in the same category: Text Manipulation & Formatting
Convert text to sentence case
Removes HTML tags and decodes a few common HTML entities
Extracts text between two delimiters
Concatenates multiple ranges or arrays and removes duplicates
Counts the total number of words within a cell or range of cells
Return uppercase initials from a name or phrase
Named functions enable the creation of custom, reusable formulas that mimic built-in functions, streamlining calculations and data manipulations. These functions simplify complex formulas, making spreadsheets more readable and less prone to errors. By encapsulating intricate logic within a single function call, they enhance consistency across your data. Utilizing named functions reduces the need to write lengthy formulas repeatedly, thereby improving workflow efficiency and productivity in data analysis and management.
Download and import — fastest way to add the function but does not include argument descriptions and examples
Follow these simple steps to download and import a function into your spreadsheet. This method is the quickest and easiest way to add the named function to your Google Sheets document, but will not include the argument description and examples.
Once added, the function will be ready to use in your document like any other built-in function. Simply type the function name and provide the required inputs to use it in your calculations.
Copy and paste — copy and paste each property one at a time
Follow these simple steps to integrate a custom named function into your spreadsheet:
Once added, the function will be ready to use in your document like any other built-in function. Simply type the function name and provide the required inputs to use it in your calculations.