VBA not calculating how I want it to

  • Thread starter Thread starter dmatador
  • Start date Start date
Click For Summary
SUMMARY

The forum discussion centers on a VBA script intended to estimate the value of pi using a summation method. The user encounters issues with the summation not functioning correctly within a Do While loop. Key problems identified include the incorrect data type for the variables 'sum' and 'n', which should be declared as Double instead of Integer to accommodate fractional values. The final output is incorrectly calculated due to these data type issues, leading to inaccurate results.

PREREQUISITES
  • Understanding of VBA (Visual Basic for Applications) programming
  • Familiarity with loops in programming, specifically Do While loops
  • Knowledge of data types in VBA, particularly Integer vs. Double
  • Basic mathematical concepts related to series and summation
NEXT STEPS
  • Learn about data types in VBA and their implications on calculations
  • Research how to properly implement loops in VBA for iterative calculations
  • Explore mathematical series for estimating pi, such as the Leibniz formula
  • Investigate debugging techniques in VBA to identify and resolve code issues
USEFUL FOR

This discussion is beneficial for VBA developers, programmers working on mathematical computations, and anyone interested in improving their skills in coding loops and data types in VBA.

dmatador
Messages
120
Reaction score
1
I've just been messing around on VBA and am trying to write a summation that estimates pi. It doesn't add anything to the sum within the do while. I tried a for loop also, but it just takes whatever i initialize sum to and multiplies it by 4 and adds one. I just want it to add continually to sum the new values of n given j. Need help it seems like such a simple problem...


Sub pi()
Dim j As Integer
Dim sum As Integer
Dim n As Integer

sum = 0
j = 2

Do While j <= 100
n = (1 / (2 * j - 1)) * ((-1) ^ (2 * j - 1))
sum = sum + n
j = j + 1
Loop
sum = 4 * sum + 1
Range("E2:E100").Cells(2).Value = sum

End Sub
 
Last edited:
Technology news on Phys.org
dmatador said:
I've just been messing around on VBA and am trying to write a summation that estimates pi. It doesn't add anything to the sum within the do while. I tried a for loop also, but it just takes whatever i initialize sum to and multiplies it by 4 and adds one. I just want it to add continually to sum the new values of n given j. Need help it seems like such a simple problem...


Sub pi()
Dim j As Integer
Dim sum As Integer
Dim n As Integer

sum = 0
j = 2

Do While j <= 100
n = (1 / (2 * j - 1)) * ((-1) ^ (2 * j - 1))
sum = sum + n
j = j + 1
Loop
sum = 4 * sum + 1
Range("E2:E100").Cells(2).Value = sum

End Sub

Your variables sum and n should not be declared as integer. There might be other problems in your code, but this jumped out at me right away.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 97 ·
4
Replies
97
Views
10K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
6K
  • · Replies 1 ·
Replies
1
Views
1K