PI in VBA code

jjokol

Board Regular
Joined
Dec 21, 2008
Messages
213
What is the correct way for PI to be used in VBA code? In the code below, I get an error message saying PI is zero:

Code:
Range("D" & topRow + 1).Offset(2, 2).Value = (count / Pi) * Range("D" & topRow + 1).Value

I've declared PI as an integer. What am I doing wrong?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Without knowing any of the context of what you are doing, declare your Pi variable as Double, not as Integer. Pi is an infinite decimalized number so no matter what you do, the result will be rounded after the 15th decimal place, but a Double type will at least be better than an Integer type.
 
Upvote 0
Thanks Tom but I'm still getting an error message for Pi - even after I've declared it as Double. The error message is number 11 - 'Division by Zero' and Pi is the offender. Its value is zero.

Surely I don't have to specify Pi's value (3.14.....) to Excel?
 
Upvote 0
Well, again, you only showed one code line from what must be a larger endeavor, but if you did not variablize Pi and you are referring it in your code as Pi with the intention of Excel interpreting it as Pi, then instead of
Pi
expand it to
WorksheetFunction.Pi
where you have it in your code.

Example, try substituting
(count / Pi)
with
(count / WorksheetFunction.Pi)
 
Upvote 0

Forum statistics

Threads
1,224,862
Messages
6,181,457
Members
453,042
Latest member
AbdelrahmanExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top