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

In following post, we will demonstrate you how to create an area converter to quickly convert from English to metric area and vice versa via Word VBA.

Unit converter can free you from painstaking manual conversion. Actually, you can totally create one in Word via VBA. And today we will show you how to build an area converter.Convert from English to Metric Area and Vice Versa via Word VBA

Create a User Form

You are going to need a user form on which your converter shall exhibit. So let’s create one first.

  1. To start off, press “Alt+ F11” in Word to trigger VBA editor.
  2. Click “Normal” and then the “Insert”.
  3. Choose “UserForm”. And resize the form if necessary.Click "Normal"->Click "Insert"->Choose "UserForm"
  4. Next press “F4” to bring out the “Properties Window” on the left.
  5. Click on user form to activate it as to set its properties in “Properties Window”.
  • Name it as “frmAreaConverter”.
  • Set caption as “Area Converter”.
  • And set “ShowModal” as “False”.
  • Configure other settings accordingly.
  1. Then click “Toolbox” on menu bar. Use controls on tool box to create 2 text boxes, 2 combo boxes, 1 label, and 2 command buttons. Arrange them in following order:Click Toolbox->Create 2 Text Boxes, 2 Combo Boxes, 1 Label and 2 Command Buttons
  2. Now you need to set properties for each control on the form.
  • First click on text box 1. Name it as “txtAreaValue1”.
  • Second click on text box 2. Name it as “txtAreaValue2”.
  • Third click on combo box 1. Name it as “cmbAreaUnit1”.
  • Then click on combo box 2. Name it as “cmbAreaUnit2”.
  • Next click on the label. You can name it as you like. But set caption text as “=”.
  • Click on command button 1. Name it as “btnConvert” and set caption as “Convert”. Set its font properly.
  • And click on command button 2. Name it as “btnClose” and set caption text as “Close”.

Enter Codes for Controls

  1. Firstly, double click on user form to enter coding space.
  2. You can see 2 combo boxes at the top of the area. Choose “Initialize” for the right one.Enter Codes for User Form
  3. Then paste following codes below:
Private Sub UserForm_Initialize()
  cmbAreaUnit1.List = Array("acre", "rood", "carucate", "bovate", "perch", _
                      "virgate", "square foot", "square inch", "square yard", _
                      "square metre", "square kilometre", "square centimetre", _
                      "square millimetre", "hectare", "square mile")
  cmbAreaUnit2.List = Array("acre", "rood", "carucate", "bovate", "perch", _
                      "virgate", "square foot", "square inch", "square yard", _
                      "square metre", "square kilometre", "square centimetre", _
                      "square millimetre", "hectare", "square mile")
End Sub
  1. Next double click on “Convert” button and enter these codes:
Private Sub btnConvert_Click()
  Dim dAreaValue1 As Double, dAreaValue1InSM As Double, dAreaValue2 As Double
  Dim strUnit1 As String, strUnit2 As String
 
  strAreaUnit1 = frmAreaConverter.cmbAreaUnit1.Text
  strAreaUnit2 = frmAreaConverter.cmbAreaUnit2.Text
 
  dAreaValue1 = CDbl(frmAreaConverter.txtAreaValue1.Value)
 
  Select Case strAreaUnit1
    Case "acre"
      dAreaValue1InSM = dAreaValue1 * 4046.8564224
    Case "rood"
      dAreaValue1InSM = dAreaValue1 * 1011.7141056
    Case "carucate"
      dAreaValue1InSM = dAreaValue1 * 485622.77069
    Case "bovate"
      dAreaValue1InSM = dAreaValue1 * 60702.846336
    Case "perch"
      dAreaValue1InSM = dAreaValue1 * 25.29285264
    Case "virgate"
      dAreaValue1InSM = dAreaValue1 * 121405.69267
    Case "square metre"
      dAreaValue1InSM = dAreaValue1
    Case "square kilometre"
      dAreaValue1InSM = dAreaValue1 * 1000000
    Case "square centimetre"
      dAreaValue1InSM = dAreaValue1 * 0.0001
    Case "square millimetre"
      dAreaValue1InSM = dAreaValue1 * 0.000001
    Case "hectare"
      dAreaValue1InSM = dAreaValue1 * 10000
    Case "square mile"
      dAreaValue1InSM = dAreaValue1 * 2589988.1103
  End Select
 
  Select Case strAreaUnit2
    Case "acre"
      dAreaValue2 = dAreaValue1InSM / 4046.8564224
    Case "rood"
      dAreaValue2 = dAreaValue1InSM / 1011.7141056
    Case "carucate"
      dAreaValue2 = dAreaValue1InSM / 485622.77069
    Case "bovate"
      dAreaValue2 = dAreaValue1InSM / 60702.846336
    Case "perch"
      dAreaValue2 = dAreaValue1InSM / 25.29285264
    Case "virgate"
      dAreaValue2 = dAreaValue1InSM / 121405.69267
    Case "square metre"
      dAreaValue2 = dAreaValue1InSM
    Case "square kilometre"
      dAreaValue2 = dAreaValue1InSM / 1000000
    Case "square centimetre"
      dAreaValue2 = dAreaValue1InSM / 0.0001
    Case "square millimetre"
      dAreaValue2 = dAreaValue1InSM / 0.000001
    Case "hectare"
      dAreaValue2 = dAreaValue1InSM / 10000
    Case "square mile"
      dAreaValue2 = dAreaValue1InSM / 2589988.1103
  End Select
 
  ' Convert dAreaValue2 to string
  If Abs(dAreaValue2 - Int(dAreaValue2)) > 0.00000001 Then
    frmAreaConverter.txtAreaValue2.Value = Format(dAreaValue2, "###0.00000000")
  Else
    frmAreaConverter.txtAreaValue2.Value = Format(dAreaValue2, "General Number")
  End If
End Sub
  1. Finally, double click “Close” button and input codes:
Private Sub btnClose_Click()
  Unload Me
End Sub

Insert a Module

  1. Still in VBA editor, click “Insert”.
  2. This time choose “Module”.Click "Normal"->Click "Insert"->Click "Module"
  3. Next double click on it to open it and paste following codes:
Sub TriggerAreaConverter()
  frmAreaConverter.Show
End Sub

Now you can run macro “TriggerAreaConverter” to trigger the area converter. Or you can assign a button for the macro. For details to do this, refer to this article: How to Remove the Formatting of Pasted Texts with Macro and VBA in Your Word

Check the demonstration below:

Manage Your Data Integrity

As individuals and businesses increasingly rely on all kinds of digital storage, the chance to get data compromised still exists. Due to natural disasters or human errors, there is a high possibility to receive your documents inaccessible. Then we are afraid that your best shot is to get a third-party tool to fix doc.

Author Introduction:

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

Comments are closed.