This post will guide you how to replace all characters after the first match of a specific character with a new text string in excel. How to replace all substrings after the first occurrence of the comma character with another substring in a text string using excel formula. How to replace all old_text with new_text in a text string.
Table of Contents
1. Replace all characters after the first comma
If you want to replace text before the first match of the comma character in a text string in Cell B1, you need to get all characters after the first comma firstly. And how to extract text after the first comma? We talked the solution in the previous post. You need to use a combination of the MID function and the FIND function to create a formula.
After getting the text after the first comma, you still need to use the SUBSTITUTE function to replace it with new_text “excelhow” in text string in Cell B1. So you need to create a formula based on the SUBSTITUTE function, the MID function and the FIND function as follows:
=SUBSTITUTE(B1,MID(B1,FIND(",",B1)+1,LEN(B1)),"excelhow",1)
Let’s see how this formul works:
=LEN(B1)
The LEN function returns the number of characters in a text string in Cell B1. The returned result goes into the MID function as its num_chars argument.
=FIND(“,”,B1)+1
The FIND function returns the position of the first comma character in Cell B1. It returns 6. And then add 1 to get the position of the first character after comma character. So this formula returns 7. The returned value goes into the MID function as its start_num argument.
=MID(B1,FIND(“,”,B1)+1,LEN(B1))
So far, you got the values of the start_num and num_chars arguments from above FIND and LEN formula. And then the MID function extracts a substring based on the starting position and the number of the characters that you want to extract from a text string in Cell B1.
=SUBSTITUTE(B1,MID(B1,FIND(“,”,B1)+1,LEN(B1)),”excelhow”,1)
This formula will replace the first old_text returned by the LEFT function with new_text “excelhow” in a text string in Cell B1.
2. Replace all characters before a specific character using FIND&Select command
You can also use the Find and Replace command to replace all characters after a specified character, just refer to the following steps:
1# Click “HOME“->”Find&Select”->”Replace…”, then the window of the Find and Replace will appear.
2# click “Replace” Tab, then type ,* into the Find what: text box, and then type “,excelhow” string in the Replace with: text box.
3# click “Replace All“
you will see that all characters after the comma character are replaced with “excelhow” string.
3. Replace all characters before a specific character using VBA Code
For our third method, we’ll explore the power of VBA. This advanced method allows for automation and customization, providing a dynamic solution to replace all characters after the first specific character. Let’s dive into the detailed steps for each approach.
Press ‘Alt + F11’ to open the Visual Basic for Applications editor.
In the editor, click ‘Insert‘ and choose ‘Module‘ to create a new module.
Copy and paste the following VBA code into the module:
Sub ReplaceAfterCharacter()
Dim sourceCell As Range
On Error Resume Next
' Prompt to select the source cell
Set sourceCell = Application.InputBox("Select the source cell:", Type:=8)
On Error GoTo 0
' Check if a cell is selected
If Not sourceCell Is Nothing Then
' Replace characters after the first specific character with "excelhow"
sourceCell.Value = Left(sourceCell.Value, InStr(1, sourceCell.Value, ",") - 1) & ",excelhow"
End If
End Sub
Close the editor and press ‘Alt + F8‘.
Choose ‘ReplaceAfterCharacter‘
and click ‘Run‘.
Once you run the macro, it will prompt you to select the source cell in your Excel worksheet. Click on the cell you want to process
press ‘OK‘
The VBA code will replace all characters after the first specific character in the selected range with “excelhow”. Adjust the code as needed for your specific requirements.
4. Video: Replace all characters before a specific character
This Excel video tutorial where we’ll uncover three diverse methods to replace all characters after the first specific character in Excel. We’ll explore a formula-based approach with SUBSTITUTE, the simplicity of the Find & Replace feature, and the advanced capabilities of VBA code.
5. Related Formulas
Replace all characters before the first specific character
If you want to replace characters before the first match of the comma character in a text string in Cell B1.you still need to use the SUBSTITUTE function to replace the old_text with new_text…
- Remove text after a specific character
If you want to remove all characters after the first match of the comma character in a text string in Cell B1, you can create an excel formula based on the LEFT function and FIND function…… - Remove text before the first match of a specific character
If you want to remove all characters that before the first occurrence of the comma character, you can use a formula based on the RIGHT function, the LEN function and the FIND function….. - Extract word that starting with a specific character
Assuming that you have a text string that contains email address in Cell B1, and if you want to extract word that begins with a specific character “@” sign, you can use a combination with the TRIM function, the LEFT function, the SUBSTITUTE function …. - Extract text before first comma or space
If you want to extract text before the first comma or space character in cell B1, you can use a combination of the LEFT function and FIND function…. - Extract text after first comma or space
If you want to get substring after the first comma character from a text string in Cell B1, then you can create a formula based on the MID function and FIND function or SEARCH function …. - Extract text before the second or nth specific character
you can create a formula based on the LEFT function, the FIND function and the SUBSTITUTE function to Extract text before the second or nth specific character… - Extract text after the second or nth specific character (space or comma)
If you want to extract text after the second or nth comma character in a text string in Cell B1, you need firstly to get the position of the second or nth occurrence of the comma character in text, so you can use the SUBSTITUTE function to replace …
6. Related Functions
- Excel Substitute function
The Excel SUBSTITUTE function replaces a new text string for an old text string in a text string.The syntax of the SUBSTITUTE function is as below:= SUBSTITUTE (text, old_text, new_text,[instance_num])…. - Excel MID function
The Excel MID function returns a substring from a text string at the position that you specify.The syntax of the MID function is as below:= MID (text, start_num, num_chars)…. - Excel FIND function
The Excel FIND function returns the position of the first text string (sub string) within another text string.The syntax of the FIND function is as below:= FIND(find_text, within_text,[start_num])… - Excel SEARCH function
The Excel SEARCH function returns the number of the starting location of a substring in a text string.The syntax of the SEARCH function is as below:= SEARCH (find_text, within_text,[start_num])… - Excel LEN function
The Excel LEN function returns the length of a text string (the number of characters in a text string).The LEN function is a build-in function in Microsoft Excel and it is categorized as a Text Function.The syntax of the LEN function is as below:= LEN(text)…
Leave a Reply
You must be logged in to post a comment.