excel - VBA中的多重替换

标签 excel vba replaceall

如何替换工作表中的多个单词?

像这样的词:Da...da, Do...do, Dos, De...de.. etc.

如何在名为“Customers3”的电子表格中进行调整?

Public Function MyProper(MyString As String, Optional exceptions As Variant)

Dim c As Variant
If IsMissing(exceptions) Then
    exceptions = Array("a", "as", "e", "o", "os", "da", "das", "de", "di", "do", "dos",  _
      "CPF", "RG", "E-Mail")
End If

MyString = Application.Proper(MyString)

For Each c In exceptions
    MyString = Replace(" " & MyString & " ", " " & c & " ", " " & LCase(c) & " ", , , vbTextCompare)
Next c

MyProper = MyString

End Sub

最佳答案

这里有一个问题:

MyString = Replace(" " & MyString & " ", " " & c & " ", " " & LCase(c) & " ", , , vbTextCompare)

每次通过循环时都会添加更多空格...

还有 End Sub 而不是 End Function

试试这个:

Public Function MyProper(MyString As String, Optional exceptions As Variant)

    Dim c As Variant
    If IsMissing(exceptions) Then
        exceptions = Array("a", "as", "e", "o", "os", "da", _
                           "das", "de", "di", "do", "dos", _
                           "CPF", "RG", "E-Mail")
    End If
    
    MyString = " " & Application.Proper(MyString) & " " 'in case exception at start/end
    
    For Each c In exceptions
        MyString = Replace(MyString, " " & c & " ", " " & LCase(c) & " ", , , vbTextCompare)
    Next c
    
    MyProper = Trim(MyString) 'remove any added spaces

End Function

关于excel - VBA中的多重替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66445608/

相关文章:

java - 替换 URL 中的符号不​​起作用

java - 正则表达式 replaceAll 的问题

excel - 如何使用 VBA 将函数插入到单元格中?

java - 无法初始化类 org.apache.poi.poifs.filesystem.FileMagic

vba - Excel VBA 项目编译后崩溃

Excel VLOOKUP N/A 错误

Excel 应用程序未从 Outlook VBA 函数关闭

excel - 如何获取当前月份?

excel - Access VBA - 触发组合框更改事件

Android RegEx 找不到匹配项