Creates a Userform that converts Amount one currency into another in Excel VBA

  Excel Interview Q&A

Today we will read on this tutorial Create a program in Excel VBA that creates a userform that converts any amount from one currency to another.

To create this user, follow these steps.  or download the excel demo file

  • Open the Visual Basic Editor. If Project Explorer is not visible, click View, Project Explorer -> Insert, click Insert. If the toolbox does not appear automatically, click View, Toolbox.
  • Add labels, list boxes (first left, second right), text boxes (first left, second right) and command buttons. Once this is completed, the result should correspond to the user’s image shown earlier. For example, create a list box control by clicking on the list box from the toolbox. Next, you can drag a list box to the user list.
  • You can change the captions of names and controls. Names are used in Excel VBA code. Captions are those that appear on your screen. It is good practice to change the names of controls, but this is not necessary here because in this example we have only a few controls. To change the caption of a userform, command button and label, click the View, Properties window, and click each control.
  • To display the userform, place a command button on your worksheet and add the following code line:
Private Sub CommandButton1_Click()

UserForm1.Show

End Sub

Now we are going to create a sub-user. When you use the show method for Userform, this sub will be executed automatically.

Open the Visual Basic Editor. -> In Project Explorer, right click on UserForm1 and then select Code -> User from the left drop-down list. Choose Initial -> from the right drop-down list. Add the following code lines:

Private Sub UserForm_Initialize()

With ListBox1
    .AddItem "Euro"
    .AddItem "Us Dollar"
    .AddItem "British Pound"
End With

With ListBox2
    .AddItem "Euro"
    .AddItem "Us Dollar"
    .AddItem "British Pound"
End With

ListBox1.ListIndex = 1
ListBox2.ListIndex = 0

TextBox1.Value = 1
TextBox2.Value = 0.722152

End Sub

We have now created the first part of Userform. Although it looks clean already, nothing happens when we click the Go button.

In Project Explorer, double click on UserForm1 -> Double click on Go button -> Add the following code lines:

Private Sub CommandButton1_Click()

Dim rates(0 To 2, 0 To 2) As Double, i As Integer, j As Integer

rates(0, 0) = 1
rates(0, 1) = 1.38475
rates(0, 2) = 0.87452

rates(1, 0) = 0.722152
rates(1, 1) = 1
rates(1, 2) = 0.63161

rates(2, 0) = 1.143484
rates(2, 1) = 1.583255
rates(2, 2) = 1

For i = 0 To 2
    For j = 0 To 2
        If ListBox1.ListIndex = i And ListBox2.ListIndex = j Then TextBox2.Value = TextBox1.Value * rates(i, j)
    Next j
Next i

End Sub
Currency Converter in Excel VBA

Currency Converter in Excel VBA

LEAVE A COMMENT