Excel函数将英文名称转换为阿拉伯文

我想添加一个VBA函数到我的Excel工作表,我可以翻译或将名称转换为阿拉伯语,以阻止他们在我的数据库。 我添加了开发人员选项卡和打开编辑器,我添加了以下function,但它不工作…请任何帮助

Function Translate_To_Arabic(str) As String ' Tools Refrence Select Microsoft internet Control Dim IE As Object, i As Long Dim inputstring As String, outputstring As String, text_to_convert As String, result_data As String, CLEAN_DATA Set IE = CreateObject("InternetExplorer.application") ' TO CHOOSE INPUT LANGUAGE inputstring = "auto" ' TO CHOOSE OUTPUT LANGUAGE outputstring = "ar" text_to_convert = str 'open website IE.Visible = False IE.navigate "http://translate.google.com/#" & inputstring & "/" & outputstring & "/" & text_to_convert Do Until IE.ReadyState = 4 DoEvents Loop Application.Wait (Now + TimeValue("0:00:5")) Do Until IE.ReadyState = 4 DoEvents Loop CLEAN_DATA = Split(Application.WorksheetFunction.Substitute(IE.Document.getElementById("result_box").innerHTML, "</SPAN>", ""), "<") For j = LBound(CLEAN_DATA) To UBound(CLEAN_DATA) result_data = result_data & Right(CLEAN_DATA(j), Len(CLEAN_DATA(j)) - InStr(CLEAN_DATA(j), ">")) Next IE.Quit transalte_using_vba = result_data End Function 

问题是你的最后一行代码应该是:

 Translate_To_Arabic = result_data 

你有错误的“翻译”

另外请确保您将引用添加到Microsoft Internet控件

如果你想用这个函数作为UDF使用这个代码:

 Function Translate_To_Arabic(Rng As Range) As String ' Tools Refrence Select Microsoft internet Control Dim IE As Object, i As Long Dim inputstring As String, outputstring As String, text_to_convert As String, result_data As String, CLEAN_DATA Set IE = CreateObject("InternetExplorer.application") ' TO CHOOSE INPUT LANGUAGE inputstring = "auto" ' TO CHOOSE OUTPUT LANGUAGE outputstring = "ar" text_to_convert = Rng.Text 'open website IE.Visible = False IE.navigate "http://translate.google.com/#" & inputstring & "/" & outputstring & "/" & text_to_convert Do Until IE.ReadyState = 4 DoEvents Loop Application.Wait (Now + TimeValue("0:00:5")) Do Until IE.ReadyState = 4 DoEvents Loop CLEAN_DATA = Split(Application.WorksheetFunction.Substitute(IE.Document.getElementById("result_box").innerHTML, "</SPAN>", ""), "<") For j = LBound(CLEAN_DATA) To UBound(CLEAN_DATA) result_data = result_data & Right(CLEAN_DATA(j), Len(CLEAN_DATA(j)) - InStr(CLEAN_DATA(j), ">")) Next IE.Quit Translate_To_Arabic = result_data End Function 

您可以在B1: =Translate_To_Arabic(A1)使用此公式在工作簿中使用此函数。 现在,如果您在单元格A1中键入book ,则会在单元格B1中看到كتاب 。 如果你想使用这个function作为加载项,请参阅此链接