VBA REPLACE 函数返回从指定字符数开始的字符串中指定的字符数。
Replace(文本、查找、替换)
或者
Replace(文本、查找、替换、开始、限制、大小写)
让我们在给定的行中执行各种替换:
Sub ReplaceExample()
text = "www.moonexcel.com.ua"
'更换简单
MsgBox Replace(text, "excel", "sheets") '返回:www.moonsheets.com.ua
'替换没有第一个字符
MsgBox Replace(text, "excel", "sheets", 5) '返回:moonsheets.com.ua
'通过指定或不指定替换次数限制来替换
MsgBox Replace(text, "e", "E", 5) '返回:moonExcEl.com.ua
MsgBox Replace(text, "e", "E", 5, 1) '返回:moonExcel.com.ua
'带或不带外壳更换
MsgBox Replace(text, "EXCEL", "sheets") '返回:www.moonexcel.com.ua
MsgBox Replace(text, "EXCEL", "sheets", , , 1) '返回:www.moonsheets.com.ua
End Sub