【问题标题】:Replace multiple characters in a string variable (VBA)替换字符串变量中的多个字符(VBA)
【发布时间】:2016-05-02 13:23:06
【问题描述】:

如何在一个字符串变量中替换多个东西?

这是我在 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

有没有更好的解决方案?

【问题讨论】:

  • 我会说“不” :-) 看起来不错

标签: excel vba str-replace


【解决方案1】:

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

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

垃圾的每个元素都可以是子字符串或单个字符。

【讨论】:

    【解决方案2】:

    如果只有几个字符,那么我会选择 Marco,但使用 VBA:

    unitNr = Replace(Replace(unitNr, "OE", ""), ";", "")
    

    【讨论】:

      【解决方案3】:

      我发现这篇文章很有帮助,所以我想我会分享我如何能够利用它来发挥自己的优势。将“Gary's Student”接受的答案与Charles Williams 的this comment 相结合,我想出了一种巧妙的方法来从任何给定的字符串中删除非数字字符。

      Public Function RemoveNonNumChars(s As String) As String
          Dim bytes() As Byte
          Dim char As String
      
          bytes = StrConv(s, vbFromUnicode)
              For Each c In bytes
                  char = chr(c)
                  If Not IsNumeric(char) Then
                      'remove character from array
                      s = Replace(s, char, "")
                      Debug.Print "Removing " & char & " from string." & Chr(13) s
                  End If
              Next c
      End Function
      

      我希望这对某人有所帮助。干杯!

      【讨论】:

      • 干得好 +:) 提示:不要忘记将 c 声明为 Variant 并向函数添加返回值:RemoveNonNumChars = s。 - 仅供参考,您可能对 remove special characters from range 的类似答案感兴趣,该答案展示了 Application.Match() 的未记录功能
      【解决方案4】:

      您可以使用正则表达式(AKA regex 和 regexp)!

      虽然默认情况下不启用正则表达式对象,但 Automate This 提供了一个非常全面的答案,解释了如何在以下链接中启用和使用它们:

      https://stackoverflow.com/a/22542835/8213085


      下面的函数是一个如何使用RegExp对象替换模式的例子,用一个子程序来说明如何使用该函数:

      Private Function RegexReplace( _
          ByVal sInput As String, _
          ByVal sPattern As String, _
          Optional ByVal sReplace As String = "" _
      )
          Dim regEx As New RegExp
      
          With regEx
              .Global = True
              .IgnoreCase = False
              .Pattern = sPattern
          End With
      
          If regEx.Test(sInput) Then
              Let RegexReplace = regEx.Replace(sInput, sReplace)
          Else
              Let RegexReplace = sInput
          End If
      
          Set regEx = Nothing
      End Function
      
      
      Private Sub Test()
          Debug.Print RegexReplace("a1b2c3", "[a-z]")  ' Prints 123
          Debug.Print RegexReplace("abc", "[a-z]")     ' Prints an empty string
          Debug.Print RegexReplace("123", "[a-z]")     ' Prints 123
          Debug.Print RegexReplace("abc", "a")         ' Prints bc
          Debug.Print RegexReplace("abc", "a", "b")    ' Prints bbc
      End Sub
      

      由于上面链接的答案非常全面,我不会解释如何构造模式——但我会注意这个自定义函数可以接受 3 个参数:

      • sInput: 搜索的字符串
      • sPattern:要搜索的正则表达式模式
      • sReplace:替换匹配字符串的可选字符串。默认为空字符串""

      示例子例程中使用的模式[a-z] 替换了a 和z 之间的任何小写字母,即每个小写字母。

      如果您只想替换 qwerty 中的字母,就像在 Gary's Student's answer 中一样,您可以提供模式 "[qwerty]":

      Private Sub Test()
          Debug.Print RegexReplace("123456qwerty", "[qwerty]")  ' Prints 123456
      End Sub
      

      Regex 真的很强大——我绝对推荐尝试使用它。默认情况下,大量现代软件开始支持它。如果您不知道要使用哪种模式,请尝试在以下网页中进行测试:

      https://regexr.com/

      【讨论】:

        【解决方案5】:

        需要VBA吗?这个公式也应该有效:

        =SUBSTITUTE(SUBSTITUTE("replace","e",""),"a","")
        

        输出将是“rpc”

        【讨论】:

        • (如果OP确实需要VBA,你可以把它变成VBA公式)
        • @marcovos 我在 VBA 中需要它。
        猜你喜欢
        • 2011-08-18
        • 2015-04-02
        • 2018-03-21
        • 2020-07-09
        • 2020-03-30
        • 2013-02-16
        • 1970-01-01
        • 1970-01-01
        • 2015-03-15
        相关资源
        最近更新 更多