I trying to make a currency converter on excel.
Cell B1 = exchange rate
cell B2 = base currency amount
cell B3 = counter currency amount.
Example:
B1 = 1.3, B2 = Euro amount, B3 = USD amount.
All three cells are connected to one another by a formula B3 = B1*B2
.
I want to be able to enter a value at B2
, and B3
automatically updates itself (B3 = B2*B1)
When I enter value at B3
, and B2
updates itself (B2 = B3/B1)
.
The below code is the best I can come up with, but it creates an out of stack space error because (I guess, it goes into an infinite loop (when B2
is updated, B3
updates itself, which in turn updates B2
again...).
Any suggestion?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim EUR As Range
Set EUR = Range("b2")
Dim USD As Range
Set USD = Range("b3")
If Not Application.Intersect(EUR, Range(Target.Address)) Is Nothing Then
Sheets("sheet1").Range("B3").Value = Range("B2") * Range("B1")
Else
If Not Application.Intersect(USD, Range(Target.Address)) Is Nothing Then
Sheets("sheet1").Range("B2").Value = Range("B3") / Range("B1")
End If
End If
End Sub