GOOGLETRANSLATE trong excel

nguyenhoa

New Member
Joined
Feb 5, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Cảm ơn sự giúp đỡ của mọi người.
Cho mình xin mã VBA mới, chạy một số mã khác toàn không được
Saasfcreenshot 2022-04-15 181026.png
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Upvote 0
You can find a simple VBA implementation below by using Microsoft XML Object Library. You will definitely hit the Google API request limitations when you have too many items. That's why the Power Query solution is much better since it is sending fewer requests by combining all cell values and splitting the response value accordingly. Also, it is easier to parse the returned JSON value in PQ.
Similar could be applied in VBA, but I suggest the Power Query method instead. Still, the following would be a good start in VBA.

Google Translated description en -> vi :
Bạn có thể tìm thấy một triển khai VBA đơn giản bên dưới bằng cách sử dụng Thư viện đối tượng XML của Microsoft. Bạn chắc chắn sẽ gặp phải các giới hạn yêu cầu API của Google khi bạn có quá nhiều mục. Đó là lý do tại sao giải pháp Power Query tốt hơn nhiều vì nó gửi ít yêu cầu hơn bằng cách kết hợp tất cả các giá trị ô và chia nhỏ giá trị phản hồi cho phù hợp. Ngoài ra, việc phân tích cú pháp giá trị JSON trả về trong PQ sẽ dễ dàng hơn.
Tương tự có thể được áp dụng trong VBA, nhưng tôi đề xuất phương pháp Power Query thay thế. Tuy nhiên, những điều sau đây sẽ là một khởi đầu tốt trong VBA.

VBA Code:
Public Function GoogleTranslate(txt, src, trg)
' Requires Microsoft XML Reference (Tool->References->Microsoft XML v6.0)
Dim xmlHttp As MSXML2.XMLHTTP60
Dim response As String
    Set xmlHttp = New MSXML2.XMLHTTP60
    With xmlHttp
        .Open "GET", "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" & src & "&tl=" & trg & "&dt=t&q=" & txt, False
        .send
        If .status = 200 Then
            response = .responseText
            response = Replace(Replace(response, "[", ""), "]", "")
            GoogleTranslate = Replace(Split(response, ",")(0), """", "")
        Else
            GoogleTranslate = CVErr(xlErrValue)
        End If
    End With
End Function
Book1
AB
1Very simple VBA implementation by using Microsoft XML Object LibraryTriển khai VBA rất đơn giản bằng cách sử dụng Thư viện đối tượng XML của Microsoft
2
3carxe ô tô
4catcon mèo
5helloxin chào
6
7xe ô tôcar
8con mèocat
9xin chàohello
Sheet1
Cell Formulas
RangeFormula
B1,B3:B5B1=GoogleTranslate(A1,"en","vi")
B7:B9B7=GoogleTranslate(A7,"vi","en")
 
Upvote 0
Solution
hi Mr. Smozgur

Your code can translate from en to vi , or vi to chinese . However , it can not work to translate from chinese to en or korea to en ,....
Could you please fix this bug and explain the reason ?
Thank you so much

 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top