How to Make a Percentage Calculator in Your Word via VBA

In this post, there are detailed steps for you to make a percentage calculator in your Word with the use of VBA editor.

A Word document can contains a lot of numbers, such as a sale report. Then you may find it such a headache to deal with percentage calculation. Therefore, we want to offer you the way to build a percentage calculator by yourself in Word.       Make a Percentage Calculator in Your Word

Insert a User Form

  1. Firstly, trigger VBA editor in Word with “Alt+ F11”.
  2. Then click “Insert” and select “UserForm”.
  3. Next press “F4” to open “Properties Window” on the down-left corner.
  4. Name the form as “frmPercentageCalculator”.
  5. And set “ShowModal” as “False”. Set other properties accordingly, such as the background.
  6. Next click “View” and choose “Toolbox”.
  7. On “Toolbox”, click “MultiPage” control.Click "View"->Choose "Toolbox"->Click "MultiPage"
  8. Adjust the control size. At first, you can see “Page1” and “Page2”. Right click on “Page2” and choose “New Page” to get “Page3” and “Page4”.
  9. Right click on “Page1” and choose “Rename”.Rename Page
  10. Rename page 1 as “Percentage(what % of)”, page 2 as “Increase/Decrease by Percentage”, page 3 as “Percentage Change”, and page 4 as “Selection % Change”.

Insert Controls into Page 1

  1. Now click on page “Percentage(what % of)”. Create four labels, three text boxes and 2 command buttons. Put them in following order:Create Four Labels and Three Text Boxes and Two Command Buttons
  2. Next set control properties.
  • First click on label 1. Set the caption as “Percentage (what % of)”. Set font and background color as you like.
  • Then click on label 2. Set the caption as “is what percentage of”.
  • Click on label 3. Set the caption as “?”.
  • And click on label 4. Set the caption as “Result:”.
  • Next click on text box 1. Name it as “txtNumerator”. And set font properly.
  • Click on text box 2. Name it as “txtDenominator”.
  • Click on text box 3. Name it as “txtPercentage”. Choose “Highlight” for “BackColor”.
  • And click on button 1. Name it as “btnCalculate”. Set the caption as “Calculate”.
  • Click on button 2. Name it as “btnInsertResult”. And set the caption as “Insert Result”.
  1. Now double click on “Calculate” button. Enter following codes:
Private Sub btnCalculate_Click()
  Dim nNumerator As Variant, nDenominator As Variant, nPercentage As Variant
 
  nNumerator = frmPercentageCalculator.txtNumerator.Text
  nDenominator = frmPercentageCalculator.txtDenominator.Text
  nPercentage = (nNumerator / nDenominator)
  frmPercentageCalculator.txtPercentage.Text = Format(nPercentage, "Percent")
End Sub
  1. Double click “Insert Result” button and paste codes:
Private Sub btnInsertResult_Click()
  Selection.InsertAfter frmPercentageCalculator.txtPercentage.Text
End Sub

Insert Controls into Page 2

  1. Click on page “Increase/Decrease by Percentage”. Then create five labels, three text boxes and two command buttons. Put them in following order:Create Five Labels and Three Text Boxes and Two Command Buttons
  2. Next set control properties.
    • First click on label 1. Set the caption as “Increase/ Decrease by Percentage”.
    • Then click on label 2. Set the caption as “Amount”.
    • Click on label 3. Set the caption as “Increase/ Decrease by”.
    • And click on label4. Set the caption as “%”.
    • Click on label 5. Set the caption as “Result:”.
    • Next click on text box 1. Name it as “txtAmount”.
    • And click on text box 2. Name it as “txtIncreaseOrDecreaseByPercentage”.
    • Click on text box 3. Name it as “txtResult”, and set the background color as “Highlight”.
    • Then click on button 1. Name it as “btnCalculateIncreasedOrDecreasedAmount”, and set the caption as “Calculate”.
    • Click on button 2. Name it as “btnInsertValue”, and set the caption as “Insert Result”.

Set font, background color and other properties if necessary.

  1. Then double click button “Calculate” and enter these codes:
Private Sub btnCalculateIncreasedOrDecreasedAmount_Click()
  Dim nAmount As Variant, nChangingPercentage As Variant, nResult As Variant
 
  nAmount = frmPercentageCalculator.txtAmount.Text
  nChangingPercentage = frmPercentageCalculator.txtIncreaseOrDecreaseByPercentage.Text
  nResult = nAmount + nAmount * nChangingPercentage * 0.01
  frmPercentageCalculator.txtResult.Text = nResult
End Sub
  1. And double click “Insert Result” button and input codes:
Private Sub btnInsertValue_Click()
  Selection.InsertAfter frmPercentageCalculator.txtResult.Text
End Sub

Insert Controls into Page 3

  1. Firstly, click on page “Percentage Change”. Next create four labels, three text boxes and two command buttons. Arrange them in following order:Create Four Labels and Three Text Boxes and Two Buttons
  2. Next go to set control properties.
  • First click on label 1. Set the caption as “Percentage Change”.
  • Then click on label 2. Set the caption as “From Value”.
  • Next click on label 3. Set the caption as “To Value”.
  • Click on label 4. Set the caption as “Result:”.
  • Next click on text box 1. Name it as “txtFromValue”.
  • And click on text box 2. Name it as “txtToValue”.
  • Click on text box 3. Name it as “txtPercentageChange” and set the background color as “Highlight”.
  • And click on button 1. Name it as “btnCalculatePercentageChange”, and set caption as “Calculate”.
  • Click on button 2. Name it as “btnInsertPercentageChange”, and set caption as “Insert Result”.
  1. Then double click button “Calculate” and paste these codes:
Private Sub btnCalculatePercentageChange_Click()
  Dim nFromValue As Variant, nToValue As Variant, nPercentageChange As Variant

  nFromValue = frmPercentageCalculator.txtFromValue.Text
  nToValue = frmPercentageCalculator.txtToValue.Text
  nPercentageChange = (nToValue - nFromValue) / nFromValue
  frmPercentageCalculator.txtPercentageChange.Text = Format(nPercentageChange, "Percent")
End Sub
  1. Double click on button “Insert Result” and paste codes:
Private Sub btnInsertPercentageChange_Click()
  Selection.InsertAfter frmPercentageCalculator.txtPercentageChange.Text
End Sub

Insert Controls into Page 4

  1. To start off, click on page “Selection % Change”. Then create two labels, three text boxes, and one button. And put them in such order:Create Two Labels and Three Text Boxes and One Button
  2. Secondly, set control properties.
  • First click on label 1. Set the caption as “Selection % Change”.
  • Next click on label 2. Set the caption as “%”.
  • Then click on text box 1. Name it as “txtDescription”. Enter following descriptive text:

Select a value in the document, then set a percentage value (add “-” if it’s negative) by which you wish to increase or decrease by.

  • Then click on text box 2. Name it as “txtPercentageValue”.
  • Next click on text box 3. Name it as “txtNote”. And input following note:

Note: This cannot be undone. Please make sure you have saved your document before performing this action.

  • And click on the button. Name it as “btnChangeSelectedValue”, and set caption as “Change Selected Value”. Highlight the background.
  1. Thirdly, double click on “Change Selected Value” button and enter codes:
Private Sub btnChangeSelectedValue_Click()
  Dim nPercentageValue As Variant, varSelectedvalue As Variant
 
  nPercentageValue = frmPercentageCalculator.txtPercentageValue.Text
  varSelectedvalue = Selection.Text
  Selection.Range.Delete
  Selection.TypeText varSelectedvalue + varSelectedvalue * nPercentageValue * 0.01
End Sub

Insert Command Button into User Form

  1. Choose “CommandButton” from “Toolbox” and create one on user form.
  2. Then name the button as “btnClose”.
  3. Set the caption text as “Close”.Create a Close Button for User Form
  4. Next double click the button and enter codes:
Private Sub btnClose_Click()
  Unload Me
End Sub

Insert a Module

  1. Click “Normal”.
  2. Then click “Insert” on toolbar and choose “Module”.Click "Normal"->Click "Insert"->Click "Module"
  3. Double click to open it and paste following codes there:
Sub CallPercentageCalculator()
  frmPercentageCalculator.Show
End Sub

Save codes and assign a button to macro “CallPercentageCalculator”. For detailed steps, you can refer to this article: How to Remove the Formatting of Pasted Texts with Macro and VBA in Your Word

Here is the demonstration of using the calculator:

Mind Your Word Issues

If your Word shows any sign of corruption, you must back up your valuable documents right away. Abnormal signs indicate there is something wrong with the software. Therefore, we suggest you use a tool to recover word.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including fix xlsx 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 *