excel表格 C列 g列 v列 从第三行开始英文转德文 vba代码
以下是将 Excel 表格中 C 列、G 列、V 列从第三行开始的英文单词转换为德文的 VBA 代码:
Sub Translate() Dim objHTTP As Object Dim strURL As String Dim strHTML As String Dim arrCells As Variant Dim i As Long
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
arrCells = Range("C3:C" & Cells(Rows.Count, "C").End(xlUp).Row).Value
For i = 1 To UBound(arrCells)
If arrCells(i, 1) <> "" Then
strURL = "https://api.mymemory.translated.net/get?q=" & arrCells(i, 1) & "&langpair=en|de"
objHTTP.Open "GET", strURL, False
objHTTP.send
strHTML = objHTTP.responseText
arrCells(i, 1) = Mid(strHTML, InStr(strHTML, """translatedText"":""") + 18, InStr(InStr(strHTML, """translatedText"":""") + 18, strHTML, """") - (InStr(strHTML, """translatedText"":""") + 18))
End If
Next i
Range("C3:C" & Cells(Rows.Count, "C").End(xlUp).Row).Value = arrCells
Set objHTTP = Nothing
arrCells = Range("G3:G" & Cells(Rows.Count, "G").End(xlUp).Row).Value
For i = 1 To UBound(arrCells)
If arrCells(i, 1) <> "" Then
strURL = "https://api.mymemory.translated.net/get?q=" & arrCells(i, 1) & "&langpair=en|de"
objHTTP.Open "GET", strURL, False
objHTTP.send
strHTML = objHTTP.responseText
arrCells(i, 1) = Mid(strHTML, InStr(strHTML, """translatedText"":""") + 18, InStr(InStr(strHTML, """translatedText"":""") + 18, strHTML, """") - (InStr(strHTML, """translatedText"":""") + 18))
End If
Next i
Range("G3:G" & Cells(Rows.Count, "G").End(xlUp).Row).Value = arrCells
Set objHTTP = Nothing
arrCells = Range("V3:V" & Cells(Rows.Count, "V").End(xlUp).Row).Value
For i = 1 To UBound(arrCells)
If arrCells(i, 1) <> "" Then
strURL = "https://api.mymemory.translated.net/get?q=" & arrCells(i, 1) & "&langpair=en|de"
objHTTP.Open "GET", strURL, False
objHTTP.send
strHTML = objHTTP.responseText
arrCells(i, 1) = Mid(strHTML, InStr(strHTML, """translatedText"":""") + 18, InStr(InStr(strHTML, """translatedText"":""") + 18, strHTML, """") - (InStr(strHTML, """translatedText"":""") + 18))
End If
Next i
Range("V3:V" & Cells(Rows.Count, "V").End(xlUp).Row).Value = arrCells
Set objHTTP = Nothing
End Sub
在代码中,我们使用了 MyMemory API 来进行翻译。你需要将代码中的 API Key 替换为你自己的 Key 才能使用。此外,如果你的电脑上没有安装 MSXML2.XMLHTTP,你需要在引用中添加对 Microsoft XML、v6.0 的引用
原文地址: https://www.cveoy.top/t/topic/cjjs 著作权归作者所有。请勿转载和采集!