我有一个字符串,我需要替换一些字符。到目前为止,我有以下vba代码,还有一些问题要问。
Dim strx As String
strx = rec("Name")
strx = Replace(strx , "a", "μ", , , 0)
strx = Replace(strx , "A", "Μ", , , 0)
strx = Replace(strx , "b", "ν", , , 0)
strx = Replace(strx , "B", "Ν", , , 0)
etc... until last letter...
我的代码正在运行,但是将大写字母弄乱到小写字母。
如何强制只将大写字母替换为某些内容并将小写字母替换为其他内容?
strx = "Alex Nero - ID12 - Age12 - no more data - Code:A5f7HMnbwi34"
正如我们在我的vba代码中看到的那样,我需要将“ A”替换为某些内容,并将“ a”替换为其他内容。代码将“ A”和“ a”弄乱了。
答案 0 :(得分:1)
Replace()
与Compare:=vbBinaryCompare
可以区分大小写字母。
Sub TestReplace()
Dim strx As String
strx = "Alex Nero - ID12 - Age12 - no more data - Code:A5f7HMnbwi34"
Debug.Print " In: " & strx
strx = Replace(strx, "a", "μ", , , vbBinaryCompare)
strx = Replace(strx, "A", "Μ", , , vbBinaryCompare)
strx = Replace(strx, "b", "ν", , , vbBinaryCompare)
strx = Replace(strx, "B", "Ν", , , vbBinaryCompare)
Debug.Print "Out: " & strx
End Sub
输出,突出显示A
和a
的不同替换方式:
In: Alex Nero - ID12 - Age12 - no more data - Code:A5f7HMnbwi34
Out: Μlex Nero - ID12 - Μge12 - no more dμtμ - Code:Μ5f7HMnνwi34
^^^^^ ^^^^^