vba 替换文本框中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7651449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:11:18  来源:igfitidea点击:

Replacing Text In a Textbox

vbauserform

提问by Russell Saari

Is there anyway to replace text in a textbox for example see below. I am currently using this, but does not seem to work well in VBA.

无论如何要替换文本框中的文本,例如见下文。我目前正在使用它,但在 VBA 中似乎效果不佳。

If TextBox6.Text.Contains("<GTOL-PERP>") Then
    TextBox6.Text = TextBox6.Text.Replace("<GTOL-PERP>", "j")
End If

回答by Banjoe

.Text is a string property in VBA. Strings are not objects in VBA so you'll need to use string functions rather than methods when dealing with them. See below:

.Text 是 VBA 中的字符串属性。字符串不是 VBA 中的对象,因此在处理它们时需要使用字符串函数而不是方法。见下文:

If instr(TextBox6.Text, "<GTOL-PERP>") Then
TextBox6.Text = replace(TextBox6.Text, "<GTOL-PERP>", "j")
End If

A List of String Functions in VBA

VBA 中的字符串函数列表

EDITYou can actually skip the IF since replace() doesn't throw an error if the text isn't in the string.

编辑您实际上可以跳过 IF,因为如果文本不在字符串中,则 replace() 不会引发错误。