在 VBA 中,Replace函数用于在字符串中查找指定的子字符串,并将其替换为新的子字符串。它的功能弹窗的替换基本一样,语法和功能比较灵活。

替换参数对比
第1点:基本语法
Replace(what, 查找文本, lookat,searchorder,matchCase,MatchByte,SearchFormat,ReplaceFormat)第2点:基本替换
Sub replacesm()
'将选定区域所有的铜刷内容更换为钢刷
Range("a1:c17").Replace what:="铜刷", replacement:="钢刷"
'将选定区域内的"xtb"列换为"new",要区分大小,不要参数不区分大小写
Range("a1:c17").Replace what:="xtb", replacement:="new", MatchCase:=True
'单元格匹配模式
Range("a1:c17").Replace what:="高温", replacement:="新款", lookat:=True
'区分全角和半角,true表明只认别半角的字符
Range("a1:c17").Replace what:="tfl", replacement:="*", matchbyte:=True
'替换文本后格式也替换
Application.ReplaceFormat.Interior.ColorIndex = 5
Range("a1:c17").Replace what:="气动管", replacement:="qdg", ReplaceFormat:=True
End Sub第3点:格式案例
找出背景色格式为红色的单元格,将背景色替换成蓝色.

格式替换
第4点:注意事项