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

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

Conversion between units from either English to metric or vice versa is frequently encountered in daily life. Therefore, you might consider building a volume converter by following steps in this post.Convert from English to Metric Volume and Vice Versa

Insert a User Form

  1. First and foremost, open VBA editor by pressing “Alt+ F11”.
  2. Then click “Insert” tab and choose “UserForm” on the drop-down menu.Click "Insert"->Click "UserForm"
  3. Next adjust the form size and trigger the “Properties Window” at the left-down corner by hitting “F4”.
  4. Click on the form and name it as “frmVolumeConverter”, set the caption text as “Volume Converter”, and also set the “ShowModal” as “False”. Besides, set other properties accordingly, such as “Font”, “BackColor”, etc.
  5. Next click “Toolbox” command on menu bar.Click Toolbox
  6. Use controls on tool box to create two text boxes, two combo boxes, two command buttons and one label on the form. And arrange these controls in following order:Create Two Text Boxes,Two Combo Boxes, Two Command Buttons and One Label
  7. Now you need to set properties of controls.
  • First click on text box 1. And name it as “txtVolumeValue1”.
  • Second, click on text box 2. And name it as “txtVolumeValue2”.
  • Next click on combo box 1. Name it as “cmbVolumeUnit1”.
  • Click on combo box 2. And name it as “cmbVolumeUnit2”.
  • Then click on label. Set its caption as “=”, and set background style as transparent.
  • Click on command button 1. Name it as “btnConvert” and set caption text as “Convert”.
  • And click on command button 2. Name it as “btnClose” and set caption text as “Close”.

Input Codes for Controls

  1. To begin with, double click on the user form to enter the coding area.
  2. There are two combo boxes at the top of the coding area. You should choose “UserForm” from the left and “Initialize” from the right.Enter codes
  3. Next you can copy and paste following codes there:
Private Sub UserForm_Initialize()
  cmbVolumeUnit1.List = Array("cubic metre", "liter", "teaspoon (metric)", "teaspoon (UK)", _
                        "tablespoon (metric)", "tablespoon (UK)", "pony", "jigger", "jack", _
                        "gill (US)", "gill (UK) ", "cup", "pint", "quart", "pottle", "gallon", _
                        "peck", "kenning", "bushel", "strike", "coomb", "oil barrel", "perch", _
                        "cord", "pin", "firkin", "kilderkin", "beer barrel", "beer hogshead", _
                        "beer pipe", "beer tun", "rundlet", "wine barrel", "tierce", _
                        "wine hogshead", "puncheon", "wine pipe", "wine tun", "cubic millimetre", _
                        "cubic centimetre", "cubic decimetre")
  cmbVolumeUnit2.List = Array("cubic metre", "liter", "teaspoon (metric)", "teaspoon (UK)", _
                        "tablespoon (metric)", "tablespoon (UK)", "pony", "jigger", "jack", _0
                        "gill (US)", "gill (UK) ", "cup", "pint", "quart", "pottle", "gallon", _
                        "peck", "kenning", "bushel", "strike", "coomb", "oil barrel", "perch", _
                        "cord", "pin", "firkin", "kilderkin", "beer barrel", "beer hogshead", _
                        "beer pipe", "beer tun", "rundlet", "wine barrel", "tierce", _
                        "wine hogshead", "puncheon", "wine pipe", "wine tun", "cubic millimetre", _
                        "cubic centimetre", "cubic decimetre")
End Sub
  1. Then switch back to the form and double click “Convert” button. Enter these codes:
Private Sub btnConvert_Click()
  Dim dVolumeValue1 As Double, dVolumeValue1InCMM As Double, dVolumeValue2 As Double
  Dim strVolumeUnit1 As String, strVolumeUnit2 As String
 
  strVolumeUnit1 = frmVolumeConverter.cmbVolumeUnit1.Text
  strVolumeUnit2 = frmVolumeConverter.cmbVolumeUnit2.Text
 
  dVolumeValue1 = CDbl(frmVolumeConverter.txtVolumeValue1.Value)
 
  Select Case strVolumeUnit1
    Case "cubic metre"
      dVolumeValue1InCMM = dVolumeValue1 * 1000000000
    Case "liter"
      dVolumeValue1InCMM = dVolumeValue1 * 1000000
    Case "dram"
      dVolumeValue1InCMM = dVolumeValue1 * 3696.6911953
    Case "teaspoon (metric)"
      dVolumeValue1InCMM = dVolumeValue1 * 5000
    Case "teaspoon (UK)"
      dVolumeValue1InCMM = dVolumeValue1 * 5919.3880208
    Case "tablespoon (metric)"
      dVolumeValue1InCMM = dVolumeValue1 * 15000
    Case "tablespoon (UK)"
      dVolumeValue1InCMM = dVolumeValue1 * 17758.164063
    Case "pony"
      dVolumeValue1InCMM = dVolumeValue1 * 29573.529563
    Case "jigger"
      dVolumeValue1InCMM = dVolumeValue1 * 44360.294344
    Case "jack"
      dVolumeValue1InCMM = dVolumeValue1 * 59147.059126
    Case "gill (US)"
      dVolumeValue1InCMM = dVolumeValue1 * 118294.11825
    Case "gill (UK)"
      dVolumeValue1InCMM = dVolumeValue1 * 142065.3125
    Case "cup"
      dVolumeValue1InCMM = dVolumeValue1 * 236588.2365
    Case "pint"
      dVolumeValue1InCMM = dVolumeValue1 * 473176.473
    Case "quart"
      dVolumeValue1InCMM = dVolumeValue1 * 946352.946
    Case "pottle"
      dVolumeValue1InCMM = dVolumeValue1 * 1892705.892
    Case "gallon"
      dVolumeValue1InCMM = dVolumeValue1 * 3785411.784
    Case "peck"
      dVolumeValue1InCMM = dVolumeValue1 * 8809767.6
    Case "kenning"
      dVolumeValue1InCMM = dVolumeValue1 * 17619535.2
    Case "bushel"
      dVolumeValue1InCMM = dVolumeValue1 * 35239070.4
    Case "strike"
      dVolumeValue1InCMM = dVolumeValue1 * 70478140.8
    Case "coomb"
      dVolumeValue1InCMM = dVolumeValue1 * 140956281.6
    Case "oil barrel"
      dVolumeValue1InCMM = dVolumeValue1 * 132489412.44
    Case "perch"
      dVolumeValue1InCMM = dVolumeValue1 * 700841953.15
    Case "cord"
      dVolumeValue1InCMM = dVolumeValue1 * 3624556363.8
    Case "pin"
      dVolumeValue1InCMM = dVolumeValue1 * 17034353.028
    Case "firkin"
      dVolumeValue1InCMM = dVolumeValue1 * 34068706.056
    Case "kilderkin"
      dVolumeValue1InCMM = dVolumeValue1 * 68137412.112
    Case "beer barrel"
      dVolumeValue1InCMM = dVolumeValue1 * 136274824.22
    Case "beer hogshead"
      dVolumeValue1InCMM = dVolumeValue1 * 204412236.34
    Case "beer pipe"
      dVolumeValue1InCMM = dVolumeValue1 * 408824472.67
    Case "beer tun"
      dVolumeValue1InCMM = dVolumeValue1 * 817648945.34
    Case "rundlet"
      dVolumeValue1InCMM = dVolumeValue1 * 68137412.112
    Case "wine barrel"
      dVolumeValue1InCMM = dVolumeValue1 * 119240471.2
    Case "tierce"
      dVolumeValue1InCMM = dVolumeValue1 * 158987294.93
    Case "wine hogshead"
      dVolumeValue1InCMM = dVolumeValue1 * 238480942.39
    Case "puncheon"
      dVolumeValue1InCMM = dVolumeValue1 * 317974589.86
    Case "wine pipe"
      dVolumeValue1InCMM = dVolumeValue1 * 476961884.78
    Case "wine tun"
      dVolumeValue1InCMM = dVolumeValue1 * 953923769.57
    Case "cubic millimetre"
      dVolumeValue1InCMM = dVolumeValue1
    Case "cubic centimetre"
      dVolumeValue1InCMM = dVolumeValue1 * 1000
    Case "cubic decimetre"
      dVolumeValue1InCMM = dVolumeValue1 * 1000000
  End Select
 
  Select Case strVolumeUnit2
    Case "cubic metre"
      dVolumeValue2 = dVolumeValue1InCMM / 1000000000
    Case "liter"
      dVolumeValue2 = dVolumeValue1InCMM / 1000000
    Case "dram"
      dVolumeValue2 = dVolumeValue1InCMM / 3696.6911953
    Case "teaspoon (metric)"
      dVolumeValue2 = dVolumeValue1InCMM / 5000
    Case "teaspoon (UK)"
      dVolumeValue2 = dVolumeValue1InCMM / 5919.3880208
    Case "tablespoon (metric)"
      dVolumeValue2 = dVolumeValue1InCMM / 15000
    Case "tablespoon (UK)"
      dVolumeValue2 = dVolumeValue1InCMM / 17758.164063
    Case "pony"
      dVolumeValue2 = dVolumeValue1InCMM / 29573.529563
    Case "jigger"
      dVolumeValue2 = dVolumeValue1InCMM / 44360.294344
    Case "jack"
      dVolumeValue2 = dVolumeValue1InCMM / 59147.059126
    Case "gill (US)"
      dVolumeValue2 = dVolumeValue1InCMM / 118294.11825
    Case "gill (UK)"
      dVolumeValue2 = dVolumeValue1InCMM / 142065.3125
    Case "cup"
      dVolumeValue2 = dVolumeValue1InCMM / 236588.2365
    Case "pint"
      dVolumeValue2 = dVolumeValue1InCMM / 473176.473
    Case "quart"
      dVolumeValue2 = dVolumeValue1InCMM / 946352.946
    Case "pottle"
      dVolumeValue2 = dVolumeValue1InCMM / 1892705.892
    Case "gallon"
      dVolumeValue2 = dVolumeValue1InCMM / 3785411.784
    Case "peck"
      dVolumeValue2 = dVolumeValue1InCMM / 8809767.6
    Case "kenning"
      dVolumeValue2 = dVolumeValue1InCMM / 17619535.2
    Case "bushel"
      dVolumeValue2 = dVolumeValue1InCMM / 35239070.4
    Case "strike"
      dVolumeValue2 = dVolumeValue1InCMM / 70478140.8
    Case "coomb"
      dVolumeValue2 = dVolumeValue1InCMM / 140956281.6
    Case "oil barrel"
      dVolumeValue2 = dVolumeValue1InCMM / 132489412.44
    Case "perch"
      dVolumeValue2 = dVolumeValue1InCMM / 700841953.15
    Case "cord"
      dVolumeValue2 = dVolumeValue1InCMM / 3624556363.8
    Case "pin"
      dVolumeValue2 = dVolumeValue1InCMM / 17034353.028
    Case "firkin"
      dVolumeValue2 = dVolumeValue1InCMM / 34068706.056
    Case "kilderkin"
      dVolumeValue2 = dVolumeValue1InCMM / 68137412.112
    Case "beer barrel"
      dVolumeValue2 = dVolumeValue1InCMM / 136274824.22
    Case "beer hogshead"
      dVolumeValue2 = dVolumeValue1InCMM / 204412236.34
    Case "beer pipe"
      dVolumeValue2 = dVolumeValue1InCMM / 408824472.67
    Case "beer tun"
      dVolumeValue2 = dVolumeValue1InCMM / 817648945.34
    Case "rundlet"
      dVolumeValue2 = dVolumeValue1InCMM / 68137412.112
    Case "wine barrel"
      dVolumeValue2 = dVolumeValue1InCMM / 119240471.2
    Case "tierce"
      dVolumeValue2 = dVolumeValue1InCMM / 158987294.93
    Case "wine hogshead"
      dVolumeValue2 = dVolumeValue1InCMM / 238480942.39
    Case "puncheon"
      dVolumeValue2 = dVolumeValue1InCMM / 317974589.86
    Case "wine pipe"
      dVolumeValue2 = dVolumeValue1InCMM / 476961884.78
    Case "wine tun"
      dVolumeValue2 = dVolumeValue1InCMM / 953923769.57
    Case "cubic millimetre"
      dVolumeValue2 = dVolumeValue1InCMM
    Case "cubic centimetre"
      dVolumeValue2 = dVolumeValue1InCMM / 1000
    Case "cubic decimetre"
      dVolumeValue2 = dVolumeValue1InCMM / 1000000
  End Select
 
  '  Convert dVolumeValue2 to string
  If Abs(dVolumeValue2 - Int(dVolumeValue2)) > 0.00000001 Then
    frmVolumeConverter.txtVolumeValue2.Value = Format(dVolumeValue2, "###0.00000000")
  Else
    frmVolumeConverter.txtVolumeValue2.Value = Format(dVolumeValue2, "General Number")
  End If
End Sub
  1. Next go back to the form and double click on “Close” button. Enter these codes:
Private Sub btnClose_Click()
  Unload Me
End Sub
  1. Lastly, save codes.

Insert a Module

  1. First off, click “Insert” tab.
  2. This time choose “Module”.Click "Normal"->Click "Insert"->Click "Module"
  3. Open it with double click and paste following codes:
Sub TriggerVolumeConverter()
  frmVolumeConverter.Show
End Sub
  1. Name the module as “modVolumeConverter”.

Now you need to assign a button for “TriggerVolumeConverter” macro. You can refer to this article for more details: How to Remove the Formatting of Pasted Texts with Macro and VBA in Your Word

You can check the demonstration video:

Take Actions in Advance

It’s always good to make full preparation in advance. Speaking of doc recovery, backups are of extreme importance. So, it’s time to generate a regular backup plan. Then if you end up with a broken document, you will have the latest version of backup.

Author Introduction:

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