替换字符链'与另一个角色链#39;

时间:2016-03-14 21:53:03

标签: excel vba excel-vba

我的小问题:(

Option Compare Text

Sub pocetpismen()

With List1

Specialchar = "á.č.ř.ž.ý"
nonspec = "a.c.r.z.y"

    delka = Len(Cells(8, 6))

    delka1 = (delka - 1) / 2

leva = Left(Cells(8, 6), delka1)
prava = Right(Cells(8, 6), delka1)

                                            Cells(26, 4) = leva 'just to help me


For Each char In Split(Specialchar, ".")
    leva = Replace(leva, char, Split(nonspec, "."))
Next

                                            Cells(25, 4) = leva 'just to help me


     If leva = prava Then
         Cells(8, 6).Interior.Color = RGB(255, 204, 0)

       ElseIf leva <> prava Then
         Cells(8, 6).Interior.Color = RGB(255, 255, 255)

     End If

End With

所以在这部分:

 For Each char In Split(Specialchar, ".")
        leva = Replace(leva, char, Split(nonspec, "."))
    Next

它没有按照我想要的顺序替换字符(它甚至没有运行)

我不想要,例如,将Specialchar = "á.č.ř.ž.ý"中定义的所有字符替换为单个字符,就像我一样

For Each char In Split(Specialchar, ".")
    leva = Replace(leva, char, "X")
Next

我希望á始终被ač更改为c,依此类推...... 任何帮助人? 提前致谢!

1 个答案:

答案 0 :(得分:0)

只需使用Range.Replace

Sub RelaceThem()
    Sheets("Sheet1").Cells.Replace What:="á", Replacement:="a", SearchOrder:=xlByColumns, MatchCase:=True, LookAt:=xlPart
    Sheets("Sheet1").Cells.Replace What:="č", Replacement:="c", SearchOrder:=xlByColumns, MatchCase:=True, LookAt:=xlPart
    Sheets("Sheet1").Cells.Replace What:="ř", Replacement:="r", SearchOrder:=xlByColumns, MatchCase:=True, LookAt:=xlPart
    Sheets("Sheet1").Cells.Replace What:="ž", Replacement:="z", SearchOrder:=xlByColumns, MatchCase:=True, LookAt:=xlPart
    Sheets("Sheet1").Cells.Replace What:="ý", Replacement:="y", SearchOrder:=xlByColumns, MatchCase:=True, LookAt:=xlPart
End Sub

为要替换的任何其他字符执行相同的代码行。请记住,这仅适用于小写字母(我有MatchCase:=True),但您可以为大写字母替换再多制作5行。