2016-09-29 28 views
0

我使用此代碼將名稱轉換爲正確大小寫,例外「von」,「af」,「de」。但它不起作用,因爲這些名字通常是「馮·埃裏克」或「弗蘭克」而不僅僅是「馮」或「af」。我怎樣才能讓excel得到這個?Excel VBA適當,但有例外

Sub ProperCase() 

    Dim rng As Range 

    'Use special cells so as not to overwrite formula. 
    For Each rng In Selection.SpecialCells(xlCellTypeConstants, xlTextValues).Cells 
     Select Case rng.Value 
      Case "von", "af", "de" 
       rng.Value = StrConv(rng.Value, vbLowerCase) 
      Case Else 
       'StrConv is the VBA version of Proper. 
       rng.Value = WorksheetFunction.Proper(rng.Value) 
     End Select 
    Next rng 

End Sub 

回答

0

你可以轉換,而不是整個小區以正確的話,那麼運行三替換功能,以取代你的話任何適當的大小寫版本:

Sub ProperCase() 

Dim rng As Range 

'Use special cells so as not to overwrite formula. 
For Each rng In Selection.SpecialCells(xlCellTypeConstants, xlTextValues).Cells 
    rng.Value = StrConv(rng.Value, vbProperCase) 
    rng.Value = Replace(Replace(Replace(rng.Value, "Von", "von"),"Af","af"), "De", "de") 
Next rng 

End Sub