Replacing

There is one built-in function and one built-in statement you can use for replacing strings.


REPLACE(expression, find, replace [,start] [,count] [,compare])

You can use the REPLACE function to return a text string with a number of characters replaced.
This built-in function is case sensitive by default.

Replace("C:\Temp\","C:\","E:\") = "E:\Temp\" 
Replace("C:\Temp\","C:\","E:\",1) = "E:\Temp\"
Replace("c:\Temp\","C:\","E:\",1) = "c:\Temp\"

If you would prefer the function to not be case sensitive, you can change the default "compare" argument.
vbCompareMethod

Replace("c:\Temp\","C:\","E:\",1,,vbCompareMethod.vbTextCompare) = "E:\Temp\" 

MID(stringvar, start [,length]) = string

You can use the MID Statement to replace a specified number of characters with characters from another string.
The number of characters replaced is always less than or equal to the number of characters in Target.
Not to be confused with the MID Function which returns a specified number of characters.

Dim sText As String 
sText = "C:\Temp\"
Mid(sText,1,1) = "E"
Debug.Print sText = "E:\Temp\"

Dim sText As String
sText = "C:\Temp\"
Mid(sText,4,4) = "Personal"
Debug.Print sText = "C:\Personal\"

Replacing the first occurrence



Replacing the last occurrence



Replacing double quotes



Replacing line break





© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext