【发布时间】:2016-02-03 17:04:50
【问题描述】:
为什么这段代码不能正常工作?任务是仅在第一次发生时从正确的位置替换(str.和strasse)。在测试中它什么都不做。
我猜错误在这一行:x.Value = StrReverse(Replace(StrReverse(x.Value), strArr(b), "straße", 1, 1)) 错误在哪里。
Sub strreplace()
Dim strArr As Variant
Dim b As Byte
Dim x As Range
strArr = Array("str.", "strasse", """")
For Each x In Selection.Cells
For b = 0 To UBound(strArr)
x.Value = StrReverse(Replace(StrReverse(x.Value), strArr(b), "straße", 1, 1))
Next b
Next x
End Sub
我测试的第二个代码是:
Sub strReplace()
Dim strArr As Variant
Dim b As Byte
strArr = Array("str.", "strasse", """")
For Each x In Selection
For b = 0 To UBound(strArr)
If InStrRev(x, strArr(b)) > 0 Then
Selection.Replace x, Replace(x, strArr(b), "straße", InStrRev(x, strArr(b)))
End If
Next b
Next
End Sub
This code transform example:
"Lessonstrasse" in straße without Lesson...
【问题讨论】:
-
只有在第一次出现时才从正确的位置 -> 你的意思是
.str或.strasse是最右边 的话在单元格值中? -
请显示您的预期输出
-
另外,在您的第一个宏中,您在
x的REVERSED 字符串中搜索strArr(b)。你永远找不到它,除非你搜索strArr(b)的反面。