How to Quickly Convert from English to Metric Length and Vice Versa via Word VBA

In this article, we are glad to show you how to quickly convert from English to metric length and vice versa via word VBA.

Frequently convert between English units and metric ones can be tedious. That being said, we have no choice but to do conversion as required. So, our topic today will offer you the method to make a length converter in Word via VBA as to save your time.    Convert from English to Metric Length and Vice Versa

Insert a User Form in VBA Project

  1. Firstly, press “Alt+ F11” to trigger VBA editor in Word.
  2. Secondly, click “Insert” tab and choose “UserForm” on that menu.
  3. Next adjust the user form size and press “F4” to open “Properties Window” at the left-down corner.
  4. Then name the form as “frmLengthConverter”, set its caption as “Length Converter”, and set the “ShowModal” as “False”.Click "Insert"->Choose "UserForm"->Set "Name", "Caption", and "ShowModal" of the Form
  5. Next click “ToolBox” command on menu bar to open the box.
  6. Then create two text boxes, two command buttons, two combo boxes, and one label on that user form. Arrange them in following order:Create Two Text Boxes, Two Combo Boxes, Two Command Buttons and One Label
  7. Now set properties of all controls created.
  • First click on text box 1. Name it as “txtValue1”.
  • Second, click on text box 2. Name it as “txtValue2”. Here we set its background as “Highlight”.
  • Next click on combo box 1. Name it as “cmbUnit1”.
  • Click on combo box 2. Name it as “cmbUnit2”.
  • And click on the label. Set the caption text as “=”.
  • Then click on command button 1. Name it as “btnConvert” and set caption text as “Convert”.
  • Similarly, click on command button 2. Name it as “btnClose”, and set caption text as “Close”.
  • Set other properties of each control if necessary.

Enter Codes for Controls

  1. Firstly, double click on user form to enter the coding space.
  2. Then at the top of coding space, there are two combo boxes. Ensure choose “UserForm” for the left and “Initialize” for the right.
  3. Next enter following codes:
Private Sub UserForm_Initialize()
  cmbUnit1.List = Array("barleycorn", "chain", "cubit", "digit", "ell", "fathom", _
                  "finger", "foot", "furlong", "hand", "inch", "league", "line", _
                  "link", "mile", "nail", "palm", "poppyseed", "rod", "shaftment", _
                  "span", "yard", "millimetre", "centimetre", "decimetre", "metre", "kilometre")
  cmbUnit2.List = Array("barleycorn", "chain", "cubit", "digit", "ell", "fathom", _
                  "finger", "foot", "furlong", "hand", "inch", "league", "line", _
                  "link", "mile", "nail", "palm", "poppyseed", "rod", "shaftment", _
                  "span", "yard", "millimetre", "centimetre", "decimetre", "metre", "kilometre")
End Sub

Choose "Initialize" for Procedure->Enter Codes

  1. And next double click “Convert” button.
  2. Paste following codes:
Private Sub btnConvert_Click()
  Dim dValue1 As Double, dValue1InMM As Double, dValue2 As Double
  Dim strUnit1 As String, strUnit2 As String
 
  strUnit1 = frmLengthConverter.cmbUnit1.Text
  strUnit2 = frmLengthConverter.cmbUnit2.Text
 
  dValue1 = CDbl(frmLengthConverter.txtValue1.Value)
 
  Select Case strUnit1
    Case "barleycorn"
      dValue1InMM = dValue1 * 8.4666666667
    Case "chain"
      dValue1InMM = dValue1 * 20116.8
    Case "cubit"
      dValue1InMM = dValue1 * 457.2
    Case "digit"
      dValue1InMM = dValue1 * 19.05
    Case "ell"
      dValue1InMM = dValue1 * 1143
    Case "fathom"
      dValue1InMM = dValue1 * 1828.8
    Case "finger"
      dValue1InMM = dValue1 * 114.3
    Case "foot"
      dValue1InMM = dValue1 * 304.8006096
    Case "furlong"
      dValue1InMM = dValue1 * 201168
    Case "hand"
      dValue1InMM = dValue1 * 101.6
    Case "inch"
      dValue1InMM = dValue1 * 25.4000508
    Case "league"
      dValue1InMM = dValue1 * 4828032
    Case "line"
      dValue1InMM = dValue1 * 2.1166666667
    Case "link"
      dValue1InMM = dValue1 * 201.168
    Case "mile"
      dValue1InMM = dValue1 * 1609347.2187
    Case "nail"
      dValue1InMM = dValue1 * 57.15
    Case "palm"
      dValue1InMM = dValue1 * 76.2001524
    Case "poppyseed"
      dValue1InMM = dValue1 * 2.1166666667
    Case "rod"
      dValue1InMM = dValue1 * 5029.2
    Case "shaftment"
      dValue1InMM = dValue1 * 152.4
    Case "span"
      dValue1InMM = dValue1 * 228.6
    Case "yard"
      dValue1InMM = dValue1 * 914.4
    Case "millimetre"
      dValue1InMM = dValue1
    Case "centimetre"
      dValue1InMM = dValue1 * 10
    Case "decimetre"
      dValue1InMM = dValue1 * 100
    Case "metre"
      dValue1InMM = dValue1 * 1000
    Case "kilometre"
      dValue1InMM = dValue1 * 1000000
  End Select
 
  Select Case strUnit2
    Case "barleycorn"
      dValue2 = dValue1InMM / 8.4666666667
    Case "chain"
      dValue2 = dValue1InMM / 20116.8
    Case "cubit"
      dValue2 = dValue1InMM / 457.2
    Case "digit"
      dValue2 = dValue1InMM / 19.05
    Case "ell"
      dValue2 = dValue1InMM / 1143
    Case "fathom"
      dValue2 = dValue1InMM / 1828.8
    Case "finger"
      dValue2 = dValue1InMM / 114.3
    Case "foot"
      dValue2 = dValue1InMM / 304.8006096
    Case "furlong"
      dValue2 = dValue1InMM / 201168
    Case "hand"
      dValue2 = dValue1InMM / 101.6
    Case "inch"
      dValue2 = dValue1InMM / 25.4000508
    Case "league"
      dValue2 = dValue1InMM / 4828032
    Case "line"
      dValue2 = dValue1InMM / 2.1166666667
    Case "link"
      dValue2 = dValue1InMM / 201.168
    Case "mile"
      dValue2 = dValue1InMM / 1609347.2187
    Case "nail"
      dValue2 = dValue1InMM / 57.15
    Case "palm"
      dValue2 = dValue1InMM / 76.2001524
    Case "poppyseed"
      dValue2 = dValue1InMM / 2.1166666667
    Case "rod"
      dValue2 = dValue1InMM / 5029.2
    Case "shaftment"
      dValue2 = dValue1InMM / 152.4
    Case "span"
      dValue2 = dValue1InMM / 228.6
    Case "yard"
      dValue2 = dValue1InMM / 914.4
    Case "millimetre"
      dValue2 = dValue1InMM
    Case "centimetre"
      dValue2 = dValue1InMM / 10
    Case "decimetre"
      dValue2 = dValue1InMM / 100
    Case "metre"
      dValue2 = dValue1InMM / 1000
    Case "kilometre"
      dValue2 = dValue1InMM / 1000000
  End Select
 
  '  Convert dValue2 to string
  If Abs(dValue2 - Int(dValue2)) > 0.00000001 Then
    frmLengthConverter.txtValue2.Value = Format(dValue2, "###0.000000")
  Else
    frmLengthConverter.txtValue2.Value = Format(dValue2, "General Number")
  End If
End Sub
  1. Next double click on “Close” button on the form and type these code lines:
Private Sub btnClose_Click()
  Unload Me
End Sub
  1. Lastly save all codes.

Insert a Module in VBA Project

  1. To start off, click “Normal”.
  2. Then again click “Insert” tab.
  3. But this time select “Module”.Click "Normal"->Click "Insert"->Click "Module"
  4. Next double click module to open it and type these codes there:
Sub TriggerLengthConverter()
  frmLengthConverter.Show
End Sub
  1. Last but not the least, save codes.

Next go to assign a button for ”TriggerLengthConverter” macro and add it to “Quick Access Toolbar” for future use. As for how to do it, you may refer to this article: How to Remove the Formatting of Pasted Texts with Macro and VBA in Your Word

Here is the video demonstration of assigning a button for macro and using the converter:

Prepare for Corruption

As an experienced user of Word, you must be aware that it’s susceptible to corruption. You might get value documents damaged but you shall always remember that data are recoverable. Therefore, it’s your choice to make to obtain an efficient tool to recover doc.

Author Introduction:

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

Comments are closed.