Insert a value before last nth character
This tutorial shows how to insert a value before the last nth character through the use of an Excel formula, with the REPLACE and LEN functions or VBA
=REPLACE(B5,LEN(B5)-3+1,0,"x")
=REPLACE(B5,LEN(B5)-C5+1,0,D5)
|
GENERIC FORMULA
=REPLACE(string,LEN(string)-nth_char+1,0,value)
ARGUMENTS GENERIC FORMULA
=REPLACE(string,LEN(string)-nth_char+1,0,value)
ARGUMENTS EXPLANATION This formula uses the REPLACE and LEN functions to insert a specific value before the last nth character in a string.
Click on either the Hard Coded or Cell Reference button to view the formula that either has the value and the number that represents before which character from the right to insert the value entered directly in the formula or referenced to cells.
In this example we are inserting a value of 'x' before the third (3rd) character from the right in a string captured in cell B5. |
Dim ws As Worksheet
ws.Range("C5").Formula = "=REPLACE(B5,LEN(B5)-3+1,0,""x"")"
End Sub
Dim ws As Worksheet
ws.Range("E5").Formula = "=REPLACE(B5,LEN(B5)-C5+1,0,D5)"
End Sub
Dim ws As Worksheet
'insert a specific value before the last nth character
ws.Range("C" & i).Formula = "=REPLACE(B" & i & ",LEN(B" & i & ")-3+1,0,""x"")"
Next i
End Sub
Dim ws As Worksheet
'insert a specific value before the last nth character
ws.Range("E" & i).Formula = "=REPLACE(B" & i & ",LEN(B" & i & ")-C" & i & "+1,0,D" & i & ")"
Next i
End Sub
Related Topic | Description | Related Topic and Description |
---|---|---|
Insert a value after first nth character | How to insert a value after the first nth character through the use of an Excel formula or VBA | |
Insert a value before first nth character | How to insert a value before the first nth character through the use of an Excel formula or VBA | |
Repeat a value n times in a cell | How to repeat a value n number of times in a cell through the use of an Excel formula or VBA |
Related Functions | Description | Related Functions and Description |
---|---|---|
LEN Function | The Excel LEN function returns the number of characters in a specified string |