Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.

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.


Download Practice Workbook

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


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.

4 Examples to Use PI (π) in Excel VBA

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 Steps 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.

Read More: How to Put Pi by Using Formula in Excel (4 Suitable Examples)


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.


Practice Section

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


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. If you have any queries or suggestions, please let us know in the comment section below. Please visit our website Exceldemy to explore more.


Related Articles

Tags:

Afia Kona

Afia Kona

Hello, I am Afia Aziz Kona. I graduated in Naval Architecture and Marine Engineering from Bangladesh University of Engineering and Technology (BUET). I have an immense interest in technical writing and content development, therefore, I am working as a content developer at Exceldemy. In my spare time, I travel, watch movies, and cook different dishes.

We will be happy to hear your thoughts

Leave a reply

ExcelDemy
Logo