适用于Excel中额外规则的案例 [英] Proper Case with extra rules in Excel

查看:96
本文介绍了适用于Excel中额外规则的案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel中使用了vba for Proper Case,但是我需要添加一个例外规则来节省大量的手动编辑。我需要 - 之后的第一个字母也被大写,例如:
michael-jordan现在当我运行我的脚本时变成Michael-jordan。
我需要迈克尔 - 约旦成为迈克尔 - 约旦。



这是我的代码:
我也有一个例外, von,af和de。

  Sub ProperCase()

Dim rng As Range

'使用特殊单元格,以免覆盖公式。
对于每个rng在Selection.SpecialCells(xlCellTypeConstants,xlTextValues).Cells
选择案例rng.Value
案例von,af,de
rng.Value = StrConv(rng.Value,vbLowerCase)
Case Else
'StrConv是正确的VBA版本。
rng.Value = StrConv(rng.Value,vbProperCase)
结束选择
下一步rng

End Sub
/ pre>

解决方案

而不是

  rng.Value = StrConv(rng.Value,vbProperCase)

使用

  rng.Value = WorksheetFunction.Proper(rng.Value)

虽然这不像Thomas Inzina所提到的那样不考虑像这样的情况


I have used vba for Proper Case in Excel but I need to add an exception rule for it to save much manual editing. I need the first letter after "-" to also be Capitalized, example is: "michael-jordan" becomes "Michael-jordan" now when I run my script. I need "michael-jordan" to become "Michael-Jordan".

Here is my code: I also have an exception for "von", "af" and "de" in my code.

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 = StrConv(rng.Value, vbProperCase)
    End Select
Next rng

End Sub

解决方案

Instead of

rng.Value = StrConv(rng.Value, vbProperCase)

use

rng.Value = WorksheetFunction.Proper(rng.Value)

Though this does not consider cases like don't doesn't as mentioned by Thomas Inzina.

这篇关于适用于Excel中额外规则的案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆