替换字符串变量(VBA)中的多个字符 [英] Replace multiple characters in a string variable (VBA)

查看:1223
本文介绍了替换字符串变量(VBA)中的多个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何替换字符串变量中的多个内容?

How can I replace more than one thing in a string variable?

这是我在VBA中的示例函数:

Here my example function in VBA:

Private Function ExampleFunc(ByVal unitNr$) As String
    If InStr(unitNr, "OE") > 0 Then
        unitNr = Replace(unitNr, "OE", "")
        unitNr = Replace(unitNr, ";", "")
    End If
...
End Function

有更好的解决方案吗?

推荐答案

您可以使用数组并在其元素上循环:

You could use an array and loop over its elements:

Sub MAIN()
    Dim s As String

    s = "123456qwerty"
    junk = Array("q", "w", "e", "r", "t", "y")

    For Each a In junk
        s = Replace(s, a, "")
    Next a

    MsgBox s
End Sub

每个垃圾元素可以是子字符串或单个字符.

Each element of junk can be either a sub-string or a single character.

这篇关于替换字符串变量(VBA)中的多个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆