VBA not calculating how I want it to

  • Thread starter dmatador
  • Start date
In summary, the conversation discusses a problem with writing a VBA code to estimate pi using a summation. The person has tried using a do while and for loop, but the code is not adding anything to the sum. They mention that the variables sum and n should not be declared as integers and ask for help in solving the issue.
  • #1
dmatador
120
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
  • #2
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.
 

Related to VBA not calculating how I want it to

1. Why is my VBA code not calculating the correct values?

There could be a few reasons for this. One possibility is that there is an error in your code, such as a typo or missing parentheses. Another possibility is that your code is not referencing the correct cells or ranges in your spreadsheet. It's also possible that there is an issue with the data itself, such as missing or incorrect values. It's important to carefully check your code and the data to identify where the issue may be occurring.

2. How can I debug my VBA code to find the calculation error?

VBA has a built-in debugging tool that allows you to step through your code and see where any errors may be occurring. To use this tool, you can set breakpoints in your code and then run it in debug mode. This will pause the code at each breakpoint, allowing you to check the values of variables and see where the code may be going wrong.

3. Why is my VBA code not updating the calculations when I change a value in my spreadsheet?

This could be due to a few different reasons. One possibility is that your code is not set to run automatically when a cell value is changed. You can change this setting by going to the Excel options, selecting "Formulas" and then checking the box for "Enable iterative calculation". Another possibility is that your code is not referencing the correct cell or range in your spreadsheet. Make sure to double check that your code is referencing the correct cells.

4. How can I ensure that my VBA code always calculates correctly?

To ensure that your VBA code always calculates correctly, it's important to thoroughly test it before using it on a large dataset. You can do this by using a smaller sample dataset and checking the results against what you expect manually. It's also important to have error handling in your code to catch any unexpected issues that may arise.

5. Is there a limit to the number of calculations that VBA can handle?

The limit to the number of calculations that VBA can handle will depend on several factors, such as the complexity of the calculations and the speed of your computer. Generally, VBA can handle a large number of calculations without issue, but if you notice your code running slowly or crashing, it may be a sign that you need to optimize your code or break it up into smaller chunks to improve performance.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
Replies
1
Views
2K
  • Programming and Computer Science
Replies
34
Views
3K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
12
Views
3K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
4K
  • Programming and Computer Science
Replies
5
Views
928
  • Programming and Computer Science
3
Replies
97
Views
7K
Replies
2
Views
1K
Back
Top