I am tasked with triaging and translating large sets of data from Chinese to English. I want to use a macro to take care of as many of the repeat terms as I possibly can. My issue is, for words that I see a lot but contain another word that is in the dictionary already in the string, it only translates the shorter word and then stops. For example, 'Name', 'Legal Name', and 'Registered Name', all appear in the data sets, however the macro only translates the Chinese 姓名, and leaves the other 3 characters in the longer word. Any help would be greatly appreciated. I'm also open to doing this in a completely different way if what I'm doing isn't the most efficient. Here is what I have;
Sub Translate()
Dim rng As Range
Dim cellArray As Variant
Dim replacements As Object
Dim key As Variant
Dim i As Long, j As Long
' Define the replacement rules using a Dictionary object
Set replacements = CreateObject("Scripting.Dictionary")
replacements.CompareMode = vbTextCompare
With replacements
.Add "性别", "Gender"
.Add "联系方式", "Contact Method"
.Add "身份证姓名", "Legal Name"
.Add "原上报姓名", "Registered Name"
.Add "姓名", "Name"
.Add "求教", "Teaching Assistant"
.Add "求教电话", "Teaching Assistant Phone Number"
End With
' Check if any cells are selected
If Selection.Cells.Count > 0 Then
' Set the range to the selected cells
Set rng = Selection
' Turn off screen updating and calculation
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Transfer range values to an array for faster processing
cellArray = rng.Value
' Perform replacements directly on the array
For i = LBound(cellArray, 1) To UBound(cellArray, 1)
For j = LBound(cellArray, 2) To UBound(cellArray, 2)
For Each key In replacements
cellArray(i, j) = Replace(cellArray(i, j), key, replacements(key), , , vbTextCompare)
Next key
Next j
Next i
' Write the modified array back to the range
rng.Value = cellArray
' Turn on screen updating and calculation
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End If
End Sub
Sub Translate()
Dim rng As Range
Dim cellArray As Variant
Dim replacements As Object
Dim key As Variant
Dim i As Long, j As Long
' Define the replacement rules using a Dictionary object
Set replacements = CreateObject("Scripting.Dictionary")
replacements.CompareMode = vbTextCompare
With replacements
.Add "性别", "Gender"
.Add "联系方式", "Contact Method"
.Add "身份证姓名", "Legal Name"
.Add "原上报姓名", "Registered Name"
.Add "姓名", "Name"
.Add "求教", "Teaching Assistant"
.Add "求教电话", "Teaching Assistant Phone Number"
End With
' Check if any cells are selected
If Selection.Cells.Count > 0 Then
' Set the range to the selected cells
Set rng = Selection
' Turn off screen updating and calculation
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Transfer range values to an array for faster processing
cellArray = rng.Value
' Perform replacements directly on the array
For i = LBound(cellArray, 1) To UBound(cellArray, 1)
For j = LBound(cellArray, 2) To UBound(cellArray, 2)
For Each key In replacements
cellArray(i, j) = Replace(cellArray(i, j), key, replacements(key), , , vbTextCompare)
Next key
Next j
Next i
' Write the modified array back to the range
rng.Value = cellArray
' Turn on screen updating and calculation
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End If
End Sub