VBA 单元格数据语言翻译代码
VBA Code for cell data language translation
我正在编写代码,将选定单元格中的数据从葡萄牙语翻译成英语,但遇到错误:
翻译后的单元格只返回 "and" 无论我写什么,它都应该翻译一个单元格中的所有单词...有什么想法吗?
这是我的代码:
Sub traducaobeta()
Dim translate As Object 'scritping.Dictionary
Set translate = CreateObject("Scripting.Dictionary")
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 ptWords As String
Dim enWords As String
ptWords = LCase(activecell.Value)
For Each tempVar In translate.Keys()
enWords = Replace(Replace(CStr(tempVar), CStr(tempVar), translate(CStr(tempVar)), InStr(CStr(tempVar), CStr(tempVar))), " e ", " and ")
activecell.Offset(0, 1).Value = enWords
Next
End Sub
有人知道怎么解决吗?
错误告诉您必须在 For Each
循环中使用 Variant
类型的变量。您使用的 ptWords
是 String
但从 translate.Keys()
返回的值不是导致错误的显式字符串类型。
要么将变量声明为变体
Dim ptWords As Variant
或者在循环中使用通用变体:
For Each tempVar In translate.Keys()
enWords = Replace(Replace(CStr(tempVar), CStr(tempVar), translate(CStr(tempVar)), InStr(CStr(tempVar), CStr(tempVar))), " e ", " and ")
activecell.Offset(0, 1).Value = enWords
Next
应该可以解决问题。
请注意,我已使用 CStr()
将 tempVar
显式转换为代码中的字符串 - 虽然这可能 总是 是必需的(由于隐式类型转换)是一个很好的实践。
我会尝试循环遍历您文本中的单词。
以下过程翻译在您的集合中找到的每个单词,并用葡萄牙语留下其他单词:
Sub traducaobeta()
Dim translate As Object 'scritping.Dictionary
Set translate = CreateObject("Scripting.Dictionary")
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.Offset(0, 1).Value = Join(Words)
End Sub
我正在编写代码,将选定单元格中的数据从葡萄牙语翻译成英语,但遇到错误:
翻译后的单元格只返回 "and" 无论我写什么,它都应该翻译一个单元格中的所有单词...有什么想法吗?
这是我的代码:
Sub traducaobeta()
Dim translate As Object 'scritping.Dictionary
Set translate = CreateObject("Scripting.Dictionary")
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 ptWords As String
Dim enWords As String
ptWords = LCase(activecell.Value)
For Each tempVar In translate.Keys()
enWords = Replace(Replace(CStr(tempVar), CStr(tempVar), translate(CStr(tempVar)), InStr(CStr(tempVar), CStr(tempVar))), " e ", " and ")
activecell.Offset(0, 1).Value = enWords
Next
End Sub
有人知道怎么解决吗?
错误告诉您必须在 For Each
循环中使用 Variant
类型的变量。您使用的 ptWords
是 String
但从 translate.Keys()
返回的值不是导致错误的显式字符串类型。
要么将变量声明为变体
Dim ptWords As Variant
或者在循环中使用通用变体:
For Each tempVar In translate.Keys()
enWords = Replace(Replace(CStr(tempVar), CStr(tempVar), translate(CStr(tempVar)), InStr(CStr(tempVar), CStr(tempVar))), " e ", " and ")
activecell.Offset(0, 1).Value = enWords
Next
应该可以解决问题。
请注意,我已使用 CStr()
将 tempVar
显式转换为代码中的字符串 - 虽然这可能 总是 是必需的(由于隐式类型转换)是一个很好的实践。
我会尝试循环遍历您文本中的单词。 以下过程翻译在您的集合中找到的每个单词,并用葡萄牙语留下其他单词:
Sub traducaobeta()
Dim translate As Object 'scritping.Dictionary
Set translate = CreateObject("Scripting.Dictionary")
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.Offset(0, 1).Value = Join(Words)
End Sub