在excel中转换整个列并将结果粘贴到下一列中

在excel中转换整个列并将结果粘贴到下一列中,excel,vba,Excel,Vba,在excel中翻译整个列并粘贴到下一列。我的代码如下: Sub test() Dim s, r As Range, l As String For Each s In Range("A2:A3").Cells l = s **Range("B2:B3").Cells = translate_using_vba(s, l)** Next s End Sub Public Function translate_using_vba(str, l) A

在excel中翻译整个列并粘贴到下一列。我的代码如下:

Sub test()
Dim s, r As Range, l As String

For Each s In Range("A2:A3").Cells
        l = s
        **Range("B2:B3").Cells = translate_using_vba(s, l)**

     Next s

End Sub


Public Function translate_using_vba(str, l) As String
' Tools Refrence Select Microsoft internet Control
    Dim IE As Object, i As Long, j 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 = "en"

    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
    translate_using_vba = result_data


End Function
子测试()
尺寸s,r为范围,l为字符串
对于范围内的每个s(“A2:A3”)。单元格
l=s
**范围(“B2:B3”)。单元格=使用vba(s,l)转换**
下一个s
端接头
使用_vba(str,l)作为字符串的公共函数translate_
'工具参考选择Microsoft internet控件
Dim IE作为对象,i作为Long,j作为Long
Dim inputstring作为字符串,outputstring作为字符串,text_to_转换为字符串,result_数据作为字符串,CLEAN_数据
设置IE=CreateObject(“InternetExplorer.application”)
'选择输入语言
inputstring=“自动”
'选择输出语言
outputstring=“en”
text\u to\u convert=str
"开放网站",
可见=假
即“导航”http://translate.google.com/#&inputstring&“/”&outputstring&“/”&text\u-to\u转换
直到IE.ReadyState=4为止
多芬特
环
Application.Wait(现在+时间值(“0:00:5”))
直到IE.ReadyState=4为止
多芬特
环
CLEAN_DATA=Split(Application.WorksheetFunction.Substitute(即.Document.getElementById(“结果框”).innerHTML,“,”,“),”)
下一个
即退出
使用_vba=结果_数据翻译_
端函数
问题:它工作正常,但我在B列中的结果每次都会被覆盖。如何先存储B2值,然后再移动到B3?

尝试以下方法:

Set r = Range("B2") ' Initial (starting) cell
For Each s In Range("A2:A3").Cells
    l = s
    r.value = translate_using_vba(s, l)
    Set r = r.Offset(1, 0) 'Next row
Next s
试试这个:

Set r = Range("B2") ' Initial (starting) cell
For Each s In Range("A2:A3").Cells
    l = s
    r.value = translate_using_vba(s, l)
    Set r = r.Offset(1, 0) 'Next row
Next s

需要改进的一个问题是:当单元格中的数据较大且有大量空格且====时,会出现运行时错误:在“r.Value=translate\u using_vba(s,l)”行出现应用程序定义或对象定义的错误。有没有办法避免它?好吧,通过单步调试代码,并利用即时窗口找出需要改进的问题-当单元格中的数据很大且有很多空间且===========,则会出现运行时错误:在“r.Value=translate\u using_vba(s,l)”行出现应用程序定义或对象定义的错误。有没有办法避免它?好吧,通过单步调试代码,并利用即时窗口找出原因