Convert KM to Miles vba code in userform

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,227
Office Version
  1. 2010
Platform
  1. Windows
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:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
The click event does not have arguments so it should just be
VBA Code:
Private Sub CommandButton1_Click()
 
Upvote 0
Yay that works Fluff thank you cant believe it was just that, thanks for your help :)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,499
Members
449,089
Latest member
Raviguru

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