VBA循环使用谷歌翻译

时间:2016-03-11 15:08:05

标签: vba loops

我有以下脚本:

s = ActiveWorkbook.Sheets("Sheet1").Range("A1").Value

ActiveWorkbook.Sheets("Sheet1").Range("B1").Value = transalte_using_vba(s)

每次A列有值时,翻译将粘贴到相应的B列中。例如,如果A7有值,则翻译将放在B7中。我希望它不会混淆。

1 个答案:

答案 0 :(得分:1)

您可以将表单UsedRange propertyFor Each循环合并,如下所示:

Sub TranslateAll()
    Dim sheet As Worksheet
    Dim inputCell As Range, outputCell As Range

    Set sheet = ActiveWorkbook.Sheets("Sheet1")

    For Each inputCell In sheet.UsedRange.Columns("A:A").Cells
        Set outputCell = sheet.Range("B" & inputCell.Row)
        If inputCell.Text > "" Then
            outputCell.Value = transalte_using_vba(inputCell.Text)
        Else
            outputCell.Value = ""
        End If
    Next
End Sub