Nếu bạn sử dụng bảng tra cứu tỷ lệ chuyển đổi, không cần VBA.
Nếu bạn định sử dụng VBA, tôi nghĩ sẽ "ưa thích nó" và giảm tỷ lệ chuyển đổi gần đây nhất. Hoặc bạn có thể làm điều đó bằng cách sử dụng một ngày cụ thể, tùy thuộc vào nơi bạn lấy dữ liệu của mình.
Làm việc thông qua điều này cũng sẽ mở rộng kiến thức của bạn về các khả năng của VBA.
Đây là một ví dụ sử dụng dữ liệu gần đây nhất từ một trang web cụ thể, nhưng có rất nhiều trên mạng. Đối với cái này, khóa api là miễn phí. Bạn sẽ phải áp dụng cho riêng bạn.
'Set reference to Microsoft winHTTP Services 5.0
'You'll need to install a JSON converter, or perhaps parse the csv output
'You could also parse this simple JSON using Regular Expressions
Option Explicit
Option Compare Text
Function ConvertInText(S As String) As String
Dim V As Variant, W As Variant
Dim DT As Date
Dim I As Long
V = Split(S, " ")
For I = 0 To UBound(V)
If V(I) = "Dollars" Then
V(I) = "Euros"
V(I - 1) = Format(USDtoEUR(CCur(V(I - 1))), "0.00")
End If
Next I
ConvertInText = Join(V)
End Function
Private Function USDtoEUR(DOL As Currency) As Currency
Const myAPI As String = "apikey=xxxxxxxxxxxxx"
Const sURL As String = "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=USD&to_currency=EUR&"
Dim httpRequest As WinHttpRequest
Dim strJSON As String, JSON As Object
Set httpRequest = New WinHttpRequest
With httpRequest
.Open "Get", sURL & myAPI
.Send
.WaitForResponse
strJSON = .ResponseText
End With
Set httpRequest = Nothing
Set JSON = parsejson(strJSON)
USDtoEUR = JSON("Realtime Currency Exchange Rate")("5. Exchange Rate") * DOL
End Function
