- VBA 教程
- VBA - 主页
- VBA - 概述
- VBA - Excel 宏
- VBA - Excel 术语
- VBA - 宏备注
- VBA - 消息框
- VBA - 输入框
- VBA - 变量
- VBA - 常量
- VBA - 运算符
- VBA - 决策
- VBA - 循环
- VBA - 字符串
- VBA - 日期和时间
- VBA - 数组
- VBA - 函数
- VBA - 子过程
- VBA - 事件
- VBA - 错误处理
- VBA - Excel 对象
- VBA - 文本文件
- VBA - 编程图表
- VBA - 用户表单
- VBA 实用资源
- VBA - 快速指南
- VBA - 实用资源
- VBA - 讨论
VBA - 替换
Replace 函数用特定字符串替换特定部分的字符串,次数可以指定。
语法
Replace(string,find,replacewith[,start[,count[,compare]]])
参数说明
String − 必需参数。需要搜索以进行替换的输入字符串。
Find − 必需参数。将被替换的字符串部分。
Replacewith − 必需参数。替换字符串,它将针对 find 参数替换。
Start − 可选参数。指定需要搜索和替换字符串的起始位置。默认值为 1。
Count − 可选参数。指定替换执行的次数。
Compare − 可选参数。指定要使用的比较方法。默认值为 0。
0 = vbBinaryCompare - 执行二进制比较
1 = vbTextCompare - 执行文本比较
示例
Private Sub Constant_demo_Click()
Dim var as Variant
var = "This is VBScript Programming"
'VBScript to be replaced by MS VBScript
msgbox("Line 1: " & Replace(var,"VBScript","MS VBScript"))
'VB to be replaced by vb
msgbox("Line 2: " & Replace(var,"VB","vb"))
''is' replaced by ##
msgbox("Line 3: " & Replace(var,"is","##"))
''is' replaced by ## ignores the characters before the first occurence
msgbox("Line 4: " & Replace(var,"is","##",5))
''s' is replaced by ## for the next 2 occurences.
msgbox("Line 5: " & Replace(var,"s","##",1,2))
''r' is replaced by ## for all occurences textual comparison.
msgbox("Line 6: " & Replace(var,"r","##",1,-1,1))
''t' is replaced by ## for all occurences Binary comparison
msgbox("Line 7: " & Replace(var,"t","##",1,-1,0))
End Sub
执行上述函数后,将输出以下内容。
Line 1: This is MS VBScript Programming Line 2: This is vbScript Programming Line 3: Th## ## VBScript Programming Line 4: ## VBScript Programming Line 5: Thi## i## VBScript Programming Line 6: This is VBSc##ipt P##og##amming Line 7: This is VBScrip## Programming
vba_strings.htm
广告
