How to Use PI(π) in Excel VBA (4 Easy Examples)

If you are looking for the usage of Pi in Excel VBA, you have come to the right place. Here, we will walk you through 4 easy examples to do the task smoothly.


What Is PI (π)?

Pi (π) is a constant number in arithmetic that is used for different calculations. Although in arithmetic, we have the value of Pi up to two decimals, that is 3.14; however, in Excel, we have a built-in PI function that stores the exact value of Pi up to 15 decimals.


Use PI (π) in Excel VBA: 4 Examples

In the following article, we will show you 4 easy usages of pi in Excel VBA. Here, we used Excel 365. You can use any available Excel version.


1. Using PI as a Single Value in Excel VBA

Here, we will use the pi as a Single value in Excel VBA. Since we will use the single value of pi, it will contain 7 figures.

Steps:

  • First of all, press ALT+F11 to bring out the VBA editor window.

At this point, you can see a VBA editor window.

  • Moreover, right-click on ThisWorkbook >> from Insert >> select Module.

  • Afterward, we will type the following code in the Module.
Sub PI_as_Single_Value()
Dim pi As Single
pi = Application.WorksheetFunction.pi()
MsgBox pi
End Sub

Using Single Value of Pi in Excel VBA

Code Breakdown

  • We declare PI_as_Single_Value as the Sub.
  • We take pi as Single.
  • We use WorksheetFunction.pi() to find out the value of pi.
  • We use a MsgBox to show the value of pi.
  • After that, close the VBA editor window >> go back to our worksheet.
  • Then, we will type ALT+F8 to run the code.

At this point, a Macro window will appear.

  • Then, we will select PI_as_Single_Value.
  • After that, click on Run.

As a result, you can see a MsgBox containing the single value of pi.

MsgBox Showing Single Vlue of Pi in Excel VBA


2. Use of PI as Double Value in Excel

In this method, we will find the value of pi when it is used as Double in Excel VBA. Since we will use the single value of pi, it will contain 15 figures. Therefore, the double value of pi gives a more precise value.

Steps:

  • In the beginning, we will follow the steps described in Method-1 to bring out the module.

After that, we will type the following code in the Module.

Sub PI_as_Double_Value()
Dim pi As Double
pi = Application.WorksheetFunction.pi()
MsgBox pi
End Sub

Use of Double Value of Pi in Excel VBA

Code Breakdown

  • We declare PI_as_Double_Value as the Sub.
  • We take pi as Double.
  • We use an WorksheetFunction.pi() to find out the value of pi.
  • We use a MsgBox to show the value of pi.
  • Then, we will close the VBA editor window >> and go back to our worksheet.
  • Moreover, we will type ALT+F8 to run the code.

At this point, a Macro window will appear.

  • Then, we will select PI_as_Double_Value.
  • After that, click on Run.

Therefore, you can see a MsgBox containing the Double value of pi.

Using MsgBox to Show Double Value of Pi in Excel VBA


3. Using PI to Calculate Circumference of a Circle

In this method, we will use pi in Excel VBA to calculate the circumference of a circle.

Here, in the following dataset, you can see the Particulars and Value of a circle.

Here, in cell C5, you can see the Radius of a circle.

Along with that, you can see the Formula for calculating the circumference in cell C6.

Next, we will find out the Circumference using VBA.

Steps:

  • In the beginning, we will follow the step described in Method-1 to bring out the Module.
  • After that, we will type the following code in the Module.
Option Explicit
Sub Circumference_of_circle()

    Dim Answer As Double
    Dim r_radius As Double
    Dim pi As Double

    pi = Application.WorksheetFunction.pi()
    r_radius = Range("C5").Value

    Answer = 2 * pi * r_radius
    Range("C7").Value = Answer

End Sub

Calculating Circumference of Circle Using Pi in Excel VBA

Code Breakdown

  • We declare Circumference_of_circle as the Sub.
  • We take r_radius and pi as Double.
  • We use WorksheetFunction.pi() to find out the value of pi.
  • We use a MsgBox to show the value of pi.
  • Range.Value method is used to extract the value of cell C5 for r_radius.
  • We use the Value method to show the result in cell C7.
  • Afterward, we will click on the Run button.

As a result, you can see the Circumference in cell C7.


4. Calculating Volume of Cylinder in Excel

In this method, we will calculate the Volume of a cylinder by using pi in Excel VBA.

Here, in cells C5 and C6 you can see the Radius and Height of the Cylinder.

Furthermore, you can see the Formula to calculate the volume of the cylinder in cell C7.

Later, we will find out the Volume by using VBA.

Steps:

  • In the beginning, we will follow the steps described in Method-1 to bring out the Module.
  • After that, we will type the following code in the Module.
Option Explicit
Sub Volume_of_cylinder()

    Dim Answer As Double
    Dim r_radius As Double
    Dim h_height As Double
    Dim pi As Single

    pi = Application.WorksheetFunction.pi()
    r_radius = Range("C5").Value
    h_height = Range("C6").Value

    Answer = pi * r_radius * r_radius * h_height
    Range("C8").Value = Answer

End Sub

Calculating Volume of Cylinder by Uisng Pi in Excel VBA

Code Breakdown

  • We declare Volume_of_cylinder as the Sub.
  • We take r_radius,h_height as Double, and pi as Single.
  • We use WorksheetFunction.pi() to find out the value of pi.
  • We use a MsgBox to show the value of pi.
  • Range.Value method is used to extract the value of cell C5 for r_radius, and C6 for h_height .
  • We use the Value method to show the result in cell C8.
  • Then, we will close the VBA editor window >> and go back to our worksheet.
  • Moreover, we will type ALT+F8 to run the code.

At this point, a Macro window will appear.

  • Then, we will select Volume_of_Cylinder.
  • After that, click on Run.

Therefore, you can see the Volume in cell C8.

Read More: How to Multiply Pi in Excel


Practice Section

You can download the above Excel file to practice the explained method.


Download Practice Workbook

You can download the Excel file and practice while you are reading this article.


Conclusion

Here, we tried to show you 4 easy usages of pi in Excel VBA. Thank you for reading this article, we hope this was helpful.


Related Articles


<< Go Back to Excel PI Function | Excel Functions | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Afia Kona
Afia Kona

Afia Aziz Kona, a graduate of Civil Engineering from Khulna University of Engineering & Technology, Bangladesh, serves as a technical content creator in the ExcelDemy project. Possessing a passion for innovation and critical thinking, she actively embraces challenges. Beyond her engineering background, Afia exhibits a keen interest in Excel, having authored numerous articles on Excel & VBA-related issues to simplify the experience for users facing obstacles in Excel. Apart from creating Excel tutorials, she is also... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo