Convert KM to Miles vba code in userform

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,081
Hi good afternoon hope you can help me please, i have a UserForm2, where i have got 6 textboxes for example textbox1 is 'Miles' so when a number is entered in this textbox then al lthe other textboxes are updated with the conversion, for example textbox is for KM. i have tried the code below but it is not working the textboxes are not updating with the conversion. hope you can help me please?

VBA Code:
Private Sub CommandButton1_Click(Index As Integer)
Dim sngMiles As Single

    'Make sure you have a mile value
    If TextBox1.Text <> "" Then
        sngMiles = Val(TextBox1.Text)
    End If
    If TextBox2.Text <> "" Then
        sngMiles = Val(TextBox2.Text) * 0.621371192
    End If
    If TextBox3.Text <> "" Then
        sngMiles = Val(TextBox3.Text) * 0.000621371192
    End If
    If TextBox4.Text <> "" Then
        sngMiles = Val(TextBox4.Text) * 0.000189393939
    End If
    If TextBox5.Text <> "" Then
        sngMiles = Val(TextBox5.Text) * 0.000568181818
    End If
    If TextBox6.Text <> "" Then
        sngMiles = Val(TextBox6.Text) * 1.15077945
    End If
  
    'Now fill in the blanks
    Calculate sngMiles
      
End Sub
Private Sub Calculate(ByVal sngMiles As Single)
    TextBox1.Text = sngMiles
    TextBox2.Text = sngMiles * 1.609344
    TextBox3.Text = sngMiles * 1609.344
    TextBox4.Text = sngMiles * 5280
    TextBox5.Text = sngMiles * 1760
    TextBox6.Text = sngMiles * 0.868976242
  
    'Add formatting
    TextBox1.Text = Format(TextBox1.Text, "#,##0.0#")
    TextBox2.Text = Format(TextBox2.Text, "#,##0.0#")
    TextBox3.Text = Format(TextBox3.Text, "#,##0.0#")
    TextBox4.Text = Format(TextBox4.Text, "#,##0.0#")
    TextBox5.Text = Format(TextBox5.Text, "#,##0.0#")
    TextBox6.Text = Format(TextBox6.Text, "#,##0.0#")
   
End Sub
 
Last edited:

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,794
Office Version
  1. 365
Platform
  1. Windows
The click event does not have arguments so it should just be
VBA Code:
Private Sub CommandButton1_Click()
 

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,081
Yay that works Fluff thank you cant believe it was just that, thanks for your help :)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,794
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,128,129
Messages
5,628,865
Members
416,347
Latest member
AT2021

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
Top