2016-03-14 70 views
1

我的小問題波紋管:(替換字符與其他字符「鏈」

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等等... 任何幫助的傢伙? 提前致謝!

回答

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行大寫字母替換。

+0

如果這適用於您,請接受綠色複選標記。 – Chrismas007

相關問題