How to Easily Convert between Various Temperature Units, such as Celsius, Fahrenheit, Kelvin, in Your Word

In this article, we will show you how to easily convert between temperature units, such as Celsius, Fahrenheit, Kelvin, in your Word.

Make good use of Word VBA, you can create a lot small but useful tools. For instance, you are absolutely fine to create a temperature converter in Word to do the tedious conversion for you. Therefore, read on to follow more details. Convert between Various Temperature Units

Insert a User Form

To create the converter, you will have to create a user form first.

  1. First of all, press “Alt+ F11” to open VBA editor.
  2. Next click “Normal” on the left column.
  3. Then click “Insert” and select “UserForm”.Click "Normal"->Click "Insert"->Click "UserForm"
  4. Click on the handles around form to adjust its size.
  5. Next press “F4” to trigger the “Properties Window”.
  6. In the window, name the user form as “frmTemperatureConverter” and set its caption as “Temperature Converter”.
  7. In addition, set the “ShowModal” property of the form as “False”.
  8. Then on the menu bar, click “Toolbox” to bring it out.
  9. Use controls on the toolbox to create a label, two text boxes, two command buttons, and two combo boxes. Put these controls on the form in following order:Create Two Text Boxes, Two Command Buttons, Two Combo Boxes and One Label
  10. Next set specific properties for controls created:
  • First click on text box 1. Name it as “txtTempValue1”.
  • Then click on text box 2 and name it as “txtTempValue2”.
  • Next click on the label and set its caption as “=”.
  • Click on combo box 1 and name it as “cmbTempUnit1”.
  • And click on combo box 2 and name it as “cmbTempUnit2”.
  • Next click on command button 1 and name it as “btnConvert”. Set caption as “Convert”.
  • Click on command button 2 and name it as “btnClose”. Set its caption as “Close”.

Besides, set other properties if necessary.

Input Codes for Controls

  1. Double click on “Convert” button to enter code area. Paste following codes there:
Private Sub btnConvert_Click()
  Dim dTempValue1 As Double, dTempValue1InF As Double, dTempValue2 As Double
  Dim strTempUnit1 As String, strTempUnit2 As String
 
  strTempUnit1 = frmTemperatureConverter.cmbTempUnit1.Text
  strTempUnit2 = frmTemperatureConverter.cmbTempUnit2.Text
 
  dTempValue1 = CDbl(frmTemperatureConverter.txtTempValue1.Value)
 
  Select Case strTempUnit1
    Case "Celsius"
      dTempValue1InF = dTempValue1 * 33.8
    Case "Fahrenheit"
      dTempValue1InF = dTempValue1
    Case "Kelvin"
      dTempValue1InF = dTempValue1 * -457.87
    Case "Rankine"
      dTempValue1InF = dTempValue1 * -458.67
    Case "Delisle"
      dTempValue1InF = dTempValue1 * 1.2
    Case "Newton"
      dTempValue1InF = dTempValue1 * 102.414
    Case "Réaumur"
      dTempValue1InF = dTempValue1 * 34.25
    Case "Rømer"
      dTempValue1InF = dTempValue1 * 3.42857143
  End Select
 
  Select Case strTempUnit2
    Case "Celsius"
      dTempValue2 = dTempValue1InF / 33.8
    Case "Fahrenheit"
      dTempValue2 = dTempValue1InF
    Case "Kelvin"
      dTempValue2 = dTempValue1InF / -457.87
    Case "Rankine"
      dTempValue2 = dTempValue1InF / -458.67
    Case "Delisle"
      dTempValue2 = dTempValue1InF / 1.2
    Case "Newton"
      dTempValue2 = dTempValue1InF / 102.414
    Case "Réaumur"
      dTempValue2 = dTempValue1InF / 34.25
    Case "Rømer"
      dTempValue2 = dTempValue1InF / 3.42857143
  End Select
 
  ' Convert dTempValue2 to string
  If Abs(dTempValue2 - Int(dTempValue2)) > 0.00000001 Then
    frmTemperatureConverter.txtTempValue2.Value = Format(dTempValue2, "###0.00000000")
  Else
    frmTemperatureConverter.txtTempValue2.Value = Format(dTempValue2, "General Number")
  End If
End Sub
  1. Next go to the form and double click “Close” button. Enter codes:
Private Sub btnClose_Click()
  Unload Me
End Sub
  1. Then double click on the form to enter it. You can see 2 combo boxes on the top. Make sure you choose “UserForm” for the left and “Initialize” for the right.Choose "UserForm" for the Left Combo Box and "Initialize" for the Right
  2. And paste codes there, too.
Private Sub UserForm_Initialize()
    cmbTempUnit1.List = Array("Celsius", "Fahrenheit", "Kelvin", "Rankine", "Delisle", _
                        "Newton", "Réaumur", "Rømer")
    cmbTempUnit2.List = Array("Celsius", "Fahrenheit", "Kelvin", "Rankine", "Delisle", _
                        "Newton", "Réaumur", "Rømer")
End Sub
  1. Lastly, save all codes.

Insert a Module

  1. Still in VBA editor, click “Insert” under “Normal” project.
  2. But this time choose “Module”.Click "Normal"->Click "Insert"->Click "Module"
  3. Double click on it to open and paste codes:
Sub TriggerTemperatureConverter()
  frmTemperatureConverter.Show
End Sub

Now run macro “TriggerTemperatureConverter” and you can trigger the converter. You can assign a button for this macro. For detailed steps, refer to this article: How to Remove the Formatting of Pasted Texts with Macro and VBA in Your Word

You can check the video below:

 

Ready for Word Collapse

No matter whether you have experienced data loss or not, you should always make preparation if data disaster occurs. Among all useful tips available, we strongly recommend getting hold of a docx recovery tool in advance.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including recover excel and pdf repair software products. For more information visit www.datanumen.com

Leave a Reply

Your email address will not be published. Required fields are marked *