+ Reply to Thread
Results 1 to 18 of 18

VBA code needed to iterate to common solution

  1. #1
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    VBA code needed to iterate to common solution

    Hi,

    I have a problem with trying to get VBA to iterate a solution for me.

    Basically, I have an initial guess value which is then used to calculate something else and then that is used to calculate the answer. This answer is then used to replace the initial guess value. How do I do this in VBA for different columns and rows?

    J (initial guess) > formula (fn(J))>J (answer)=J(inital guess) and so on...

    Can someone help?

  2. #2
    Forum Contributor amartinez988's Avatar
    Join Date
    05-04-2015
    Location
    Miami
    MS-Off Ver
    Office 2010
    Posts
    183

    Re: VBA code needed to iterate to common solution

    Hi Hassanm and welcome the forum.

    Please be more specific in what you want. Maybe posting the workbook and captions will help.

    Thanks,

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,879

    Re: VBA code needed to iterate to common solution

    exacty as you wrote above

    so if your initial guess is in a1 and formula in B1 you can try:
    Please Login or Register  to view this content.
    this way you do 100 iterations.
    Best Regards,

    Kaper

  4. #4
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    Re: VBA code needed to iterate to common solution

    As you can see I have done the VBA code you mentioned Kaper.

    amartinez988. You can see what I need in more detail. The initial guess is used to feed into the column on the right (C3). C3 is a function of D3. D3 is a function of E3 and finally F3 is a function of B3.

    I need to solve this so that B3 and F3 match. And I also need to do this for multiple rows and columns.
    Attached Files Attached Files

  5. #5
    Forum Contributor amartinez988's Avatar
    Join Date
    05-04-2015
    Location
    Miami
    MS-Off Ver
    Office 2010
    Posts
    183

    Re: VBA code needed to iterate to common solution

    I don't quite understand the formulas that you are putting but the complete formula to replace that in only one instead of using cell by cell is:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Let us know

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,879

    Re: VBA code needed to iterate to common solution

    Hi hassanm,
    To make it that way, your function shall be convergent (hope it's the right term - never had math classes in English).
    And the one you have shown do not look to be convergent, because your answer is =3,75*(1-initial_guess)

    Not getting deeper into math you get: positive - negative - positive - negative .... sequence, while absolute value will be bigger and bigger every step.

  7. #7
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    Re: VBA code needed to iterate to common solution

    Quote Originally Posted by amartinez988 View Post
    I don't quite understand the formulas that you are putting but the complete formula to replace that in only one instead of using cell by cell is:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Let us know
    Yes they could be replaced with one long equation but I need it cell by cell as they are variables I need to show.

    How do I create a macro to allow me to iterate B3=F3 over different rows and columns?

  8. #8
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,879

    Re: VBA code needed to iterate to common solution

    If you need just B3=F3 then simply solve the equation. B3 (=F3) will be just 15/19 (0.78947368....)

    PS. see also my second post above (you may not noticed it).

  9. #9
    Forum Contributor amartinez988's Avatar
    Join Date
    05-04-2015
    Location
    Miami
    MS-Off Ver
    Office 2010
    Posts
    183

    Re: VBA code needed to iterate to common solution

    So you want to substitute the value in column B by the one obtained on column F? The first value in B always going to be replaced by the last obtained in F?

  10. #10
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    Re: VBA code needed to iterate to common solution

    I have produced a sample excel workbook to hopefully provide a better idea of what I need. You can see that equation 8 is the answer whilst the initial guesses is used to calculate equation 2 which leads to an answer given in equation 8.

    So D7 (initial guess) gives an answer of provided in cell D112. I would a VBA code that will be able to iterate this to give me a solution where D7 and B112 match. And apply it to the rest of the cells (column D:N).
    Attached Files Attached Files

  11. #11
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,879

    Re: VBA code needed to iterate to common solution

    In my opinion with this approach, there is no chance to solve it, because you in 4 of your equations use random numbers generator.
    So each recalculation will change the result.

    Moreover again absolute base maths seems to be a problem.
    Your equation 8 (D112) is:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    while equation 7 (D99) was:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    equation 6 -> D86 (one of random values! is used only in 2 above equations). So just write in D112:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    and you can delete rows 85 to 110.
    the same step by step up, and finally you have only
    1) your constants in column B (by the way, have you noted that you never use const C - cell B5? - well this case it does not matter because the whole appoach is wrong.)
    2) your initial values (you used all the same)
    3) just one equation:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    if not the last part - could be easily solved, but as with every calculation the jast part changes randomly:
    no way.

    Side comment:
    this last part:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    can be pretty well fitted with simple normal (gaussian) dustribution of standard deviation 0.5 and expected value of -0.5.
    Of course with real gaussian one can go below -2 or above 1 (which are limits for 2 negative random and one positive).
    so if one do not mind thin tails going out of (-2;1) range, such approach could be used:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Last edited by Kaper; 07-08-2015 at 10:40 AM.

  12. #12
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    Re: VBA code needed to iterate to common solution

    Quote Originally Posted by Kaper View Post
    exacty as you wrote above

    so if your initial guess is in a1 and formula in B1 you can try:
    Please Login or Register  to view this content.
    this way you do 100 iterations.
    I have tried to use this iteration but I do not know how to apply it to different cells. I have a range of cells. Range (BP393:DX432) equate to (F3:BM42).
    I have done the first one where I match BP393 to F3 as shown below and I have attempted to do it for the range. This does not work?

    Please Login or Register  to view this content.

  13. #13
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,879

    Re: VBA code needed to iterate to common solution

    Please Login or Register  to view this content.
    if F3:... are initial values.
    But please read through my comments above.

    Math is not only to make students life harsh.
    It is painful torture tool for those who try to calculate something not following math rules.

  14. #14
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    Re: VBA code needed to iterate to common solution

    The code works perfectly for the first column i.e. iterating BP393:432 to match F3:F42 but it does not seem to work for the remaining columns.

    The code is this atm:
    Please Login or Register  to view this content.
    BP393:DX432 being the initial guess

  15. #15
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,879

    Re: VBA code needed to iterate to common solution

    note that your ranges have different numbers of columns - 61 and 60 resp.
    Please Login or Register  to view this content.
    correct it first.

  16. #16
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    Re: VBA code needed to iterate to common solution

    I have corrected to :

    Please Login or Register  to view this content.
    Am I right in suggesting that this piece of code will iterate 100 times and find a solution whereby BQ393 = G3, BR393 = H3 and so on across the whole row as well as find the solution down the column i.e. BQ393 = G3, BQ394 = G4 and so on.

  17. #17
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,879

    Re: VBA code needed to iterate to common solution

    yes. 100 times.

  18. #18
    Registered User
    Join Date
    07-07-2015
    Location
    England
    MS-Off Ver
    MS Office 2010
    Posts
    69

    Re: VBA code needed to iterate to common solution

    Many thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. iterate code through all open files
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-05-2015, 02:02 PM
  2. [SOLVED] VBA code will not iterate cell values.
    By xizor in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-10-2014, 05:56 PM
  3. Solution needed for VBA learning
    By ronanm in forum The Water Cooler
    Replies: 3
    Last Post: 03-09-2011, 12:47 PM
  4. Looking for a solution to a common problem
    By t1mmclaren in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-17-2009, 05:30 AM
  5. [SOLVED] UCase Code Solution Needed
    By Phil H. in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-05-2005, 12:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1