语言翻译代码中的VBA大小写修复

时间:2016-05-12 15:11:06

标签: excel excel-vba uppercase language-translation vba

我的翻译代码没有按照我想要的方式工作......它应该仅在单元格的第一个单词中执行Propercase,但它在单元格中的所有单词中都是正确的。

关于如何翻译并且仅在activecell中的第一个单词上使用大写字母的任何想法?

以下是代码:

 Sub traducaobeta2()

 Dim translate As Object 'scritping.Dictionary

Set translate = CreateObject("Scripting.Dictionary")

translate("cadeira") = "chair"
translate("cadeira,") = "chair"
translate("cadeiras") = "chairs"
translate("criado mudo") = "night stand"
translate("criado-mudo") = "night stand"
translate("mesa") = "table"
translate("mesas") = "tables"
translate("e") = "and"
' the list goes on...


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)
For Each x In activecell
x.Value = Application.Proper(x.Value)
Next
activecell.Offset(0, 1).Select

End Sub

1 个答案:

答案 0 :(得分:2)

将第一个字母设为首字母:

ActiveCell.value = UCase$(Left$(ActiveCell.value, 1)) & Right$(ActiveCell.value, Len(ActiveCell.value) - 1)

还可以使用With块来保存输入:

With ActiveCell
    .value = UCase$(Left$(.value, 1)) & Right$(.value, Len(.value) - 1)
End With