为什么这段代码会使代码更改为正确的大小写?

时间:2016-05-18 17:21:59

标签: vba excel-vba case-sensitive case-insensitive excel

此代码正在翻译单元格中的所有单词,但只有第一个单词应强制为正确的大小写,其他单词应保持用户写的大小写,而是强制第一个单词为正确的大小写小区中的其他单词为小写。所有其他的话应该保留原来的情况。

Sub TraAdd()

    Dim translate As Object 'scritping.Dictionary

    Set translate = CreateObject("Scripting.Dictionary")

    translate("modens") = "modems"
    translate("Modens") = "Modems"
    translate("modens,") = "modems,"
    translate("Modens,") = "Modems,"
    translate("Fruteira,") = "Fruit bowl,"
    translate("fruteira,") = "fruit bowl,"
    translate("Fruteira") = "Fruit bowl"
    translate("fruteira") = "fruit bowl"
    translate("muletas") = "crutches"
    translate("Muletas") = "Crutches"
    translate("muletas,") = "crutches,"
    translate("Muletas,") = "Crutches,"


    Dim Words As Variant
    Dim I As Integer
    Words = Split(LCase(activecell.Value))


    For I = LBound(Words) To UBound(Words)
        If translate(Words(I)) <> "" Then Words(I) = translate(Words(I))
    Next
    activecell.Value = Join(Words)
    activecell.Value = Ucase$(Left$(activecell.Value, 1)) & Right$(activecell.Value, Len(activecell.Value) - 1)

End Sub

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

将所有内容拆分为数组后,您已将所有内容设为小写。

在将单元格内容拆分为LCase时删除Words,它应该按照您的意图运行:

Words = Split(activecell.Value)