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

Example 1 – Using PI as a Single Value in Excel VBA

We will use the pi as a Single value in Excel VBA. It will contain 7 figures.

Steps:

  • Press ALT+F11 to bring out the VBA editor window.

A VBA editor window will appear.

  • Right-click on ThisWorkbook >> from Insert >> select Module.

  • Enter 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.
  • Close the VBA editor window >> go to our worksheet.
  • Press ALT+F8 to run the code.

A Macro window will appear.

  • Select PI_as_Single_Value.
  • Click on Run.

This results in a MsgBox containing the single value of pi.

MsgBox Showing Single Vlue of Pi in Excel VBA


Example 2 – Use of PI as Double Value in Excel

Steps:

  • Follow the steps in Example 1 to bring out the module.

Enter the following code.

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.
  • Close the VBA editor window >>go to our worksheet.
  • Press ALT+F8 to run the code.

A Macro window will appear.

  • Select PI_as_Double_Value.
  • Click on Run.

This results in a MsgBox containing the Double value of pi.

Using MsgBox to Show Double Value of Pi in Excel VBA


Example 3 – Using PI to Calculate Circumference of a Circle

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

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

You can see the Formula for calculating the circumference in cell C6.

We will find out the Circumference using VBA.

Steps:

  • Follow the step in Example 1 to open the Module.
  • Enter the following code.
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.
  • Click on Run.

The Circumference result is in cell C7.


Example 4 – Calculating Volume of Cylinder in Excel

Steps:

  • Follow the steps in Example 1 to bring up the Module.
  • Enter the following code.
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.
  • Close the VBA editor window >> go to our worksheet.
  • Press ALT+F8 to run the code.

A Macro window will appear.

  • Select Volume_of_Cylinder.
  • Click on Run.

The Volume result is in cell C8.

Read More: How to Multiply Pi in Excel


Download Practice Workbook


Related Article


<< 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 NAME from Bangladesh 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 interested... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo