In mathematics, Pi is an irrational number (a decimal with no end) that is equal to roughly 3.14.
To use Pi in calculations in VBA, you can use the following syntax:
Function MultiplyByPi(inputVal)
Dim Pi As Double
MultiplyByPi = inputVal * Application.WorksheetFunction.Pi()
End Function
This particular function allows you to multiply any value in Excel by Pi.
Note that Application.WorksheetFunction.Pi returns the value 3.14159265358979, which is the constant Pi accurate to 15 digits.
If you need a calculation to be more accurate than 15 digits, you can simply hard code Pi to as many digits as you would like.
The following example shows how to use this syntax in practice.
Example: How to Use Pi in VBA
Suppose we have the following list of values in Excel that we would like to multiply by Pi:
To do so, we can create the following function in VBA:
Function MultiplyByPi(inputVal)
Dim Pi As Double
MultiplyByPi = inputVal * Application.WorksheetFunction.Pi()
End Function
Once we’ve created this function, we can then type the following formula into cell B2 to multiply the value in cell A2 by Pi:
=MultiplyByPi(A2)
We can then click and drag this formula down to each remaining cell in column B:
Column B now shows each value in column A multiplied by Pi.
Additional Resources
The following tutorials explain how to perform other common tasks in VBA:
VBA: How to Sum Values in Range
VBA: How to Calculate Average Value of Range
VBA: How to Count Number of Rows in Range