2016-09-29 52 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