VBA Code - Creating loop to solve iterative solution issue

  • My workbook currently has a cell [e37]) "Neutral Axis" which requires the user to manually input a value until cells [g39] "Ctotal" and [g40] "Ttotal" equal each other. Both Ctotal and Ttotal change based on the value of the Neutral Axis.

    I am attempting to solve this iterative problem using a "Do Until" loop by stepping the neutral axis value by 0.1 until the absolute value of "Ctotal - Ttotal" is less than 10.

    However when I run my code excel freezes up and eventually crashes. I have run the code with a hard value for the neutral axis and came up with the correct values so I believe the issue is with the Do until loop somewhere.

    Any help is appreciated!

  • Hi JB,

    There are no changes occurring in the loop, the values all remain the same and subsequently i never changes from a value of I think 342 and the loop is infinite. I am not really sure what you are attempting to do so cannot really help much more.

    To see what is going on click on the side of your code so a red mark appears in the border, place this within the loop then press play and the code will stop running at that point. Ensure your locals window is open and the value of all your variables will be there. Press F8 to then step through the code (hold F8 to rapidly move through the loops) I added a counter for the loops (x = x + 1) to keep track, but nothing changes with each iteration.

    NB: I have been using VBA a long time and never seen that method of referencing cells before, it is brief but mouse-over with codebreak does not provide the current value, it will also cause problems if you have other than the sheet of interest active (i.e. the code will not know which sheet to reference or I assume that is the case).



Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!