如何替换工作表中的多个单词?
像这样的词: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/