+ Reply to Thread
Results 1 to 15 of 15

circular reference within a cell using VBA

  1. #1
    Registered User
    Join Date
    03-27-2017
    Location
    Germany
    MS-Off Ver
    office 365 pro plus
    Posts
    38

    circular reference within a cell using VBA

    Hi all,

    I am running into a Problem regarding circular reference in Excel vba. I am trying to assign a cell, a formula which requires the mentioning of the same cell.
    The equation is

    f=1/-2*LOG(3.7*k/D + 2.5/Re*sqrt(f))^2
    where k and D are constants. As it can be seen, the f appears on both sides of the equation and Needs to be calculated. When I transform above equation to assign as a formula in a cell (say A1), it reads

    =1/(-2*LOG(k/D*3,7) + 2,51/(D39*WURZEL(A1+1E-300))))^2

    The above formula seems to be working, giving the value of f but everytime i Change the constant, it Shows an error and i Need to go to the formula bar to hit enter again and the value appears again. This is obv undesirable so I thought of writing a Macro which read as follows

    ActiveCell.Formula = 1 / (-2 * Log(k/D * 3.7) + (2.51 / Re * Sqr(R1C1 + 1E-300))) ^ 2

    surprisingly the above macro does not work at all giving an error.

    Any help would be highly appreciated.

    Cheers!!!

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,064

    Re: circular reference within a cell using VBA

    Don't VBA .Formulas have to be in double quotes?
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    Registered User
    Join Date
    03-27-2017
    Location
    Germany
    MS-Off Ver
    office 365 pro plus
    Posts
    38

    Re: circular reference within a cell using VBA

    Thanks for your reply. Even if i use activecell.value it keeps giving an error. It Highlights the part ''SQRT'' and give san error Sub or function not defined.

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: circular reference within a cell using VBA

    You need it in double quotes to treat it as string. Otherwise VBA is trying to interpret it as VBA function and since it does not exist... gives above error.

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: circular reference within a cell using VBA

    It does not seem like VBA should be necessary for this. A couple of suggestions.

    I don't like single cell circular references like this. I would spread this calculation out over two cells (don't worry, you have 1E6 by 16000 cells in a single tab, you are not going to run out of cells). In the first cell (A1), the formula would be something like =second cell (=B1). The second cell (B1) then become =1/(-2*LOG(k/D*3,7) + 2,51/(D39*WURZEL(A1+1E-300))))^2

    As I noted in your previous thread (https://www.excelforum.com/excel-gen...ba-macros.html ), I usually include some kind of reset on error (usually an IFERROR() function) in my circular references/iterative calculations. I would try that. Something like =IFERROR(second cell,typical value for f) (=IFERROR(B1,0.5) maybe). I really don't know if 0.5 is a good typical value for f, but I assume that you know what f will typically converge to and can supply that value.

    If you looked at the Newton-Raphson and other examples of circular references I have on the forum, I also frequently include some other reset toggle in my circular references, so I can reset the calculation manually. You may want to consider that as well.

    I would try those two suggestions before resorting to some VBA trickery to get this circular reference to work.
    Last edited by MrShorty; 05-04-2017 at 08:50 AM.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  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,623

    Re: circular reference within a cell using VBA

    moreover - if you use RC style of addressing, you probably shall use formular1c1 not just formula? and final comment square root in formulas is SQRT. so try:


    And going back to basics: The situation you described: "but everytime i Change the constant, it Shows an error and i Need to go to the formula bar to hit enter again and the value appears again" shall not happen and would be worth to work on it.

    Please Login or Register  to view this content.
    PS. Note that I changed k to k_ - in my version of excel k is not valid name for name manager (Ctrl+F3). May be in your version of Excel (looking on a sqrt function name - you use a German one) - it is valid.

    edit: started answering, and had to go out for few minutes, so my answer partially repeats earlier comments. As I am editing a post I'd add also more administrative comment - please use code tags - see rule 3 in https://www.excelforum.com/forum-rul...rum-rules.html and edit your post accordingly
    Last edited by Kaper; 05-04-2017 at 08:55 AM.
    Best Regards,

    Kaper

  7. #7
    Registered User
    Join Date
    03-27-2017
    Location
    Germany
    MS-Off Ver
    office 365 pro plus
    Posts
    38

    Re: circular reference within a cell using VBA

    Thanks for your Responses. Unfortunately, I still have not been able to get fix the proble.
    As mentioned by MrShorty, I kinda have to use VBA since the bulk of the calculations are being carried out using vba. I tried to set A1=B1 and put formula in B1 referencing A1 as circular reference as you suggeseted but it did nt give the accurate value for some odd reason.

    As per Kaper Suggestion, I used the formula that he mentioned with variable_Re (not really a variable but a preceeding value from the previous row). But still getting the error. Please have a look at the screenshot attached.
    Attached Images Attached Images

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: circular reference within a cell using VBA

    "ActiveCell.FormulaR1C1" this part does not change.

  9. #9
    Registered User
    Join Date
    03-27-2017
    Location
    Germany
    MS-Off Ver
    office 365 pro plus
    Posts
    38

    Re: circular reference within a cell using VBA

    This is the issure i am facing. When normally inserting the formula into a cell, it calculates the value without a fuss. Even if i try with the GoakSeek function (without VBA) it give the correct value but as have to write a code in vba to calculate this value i am either getting an error or sometimes it does calculate the value which is not correct (not exactly what the above mentioned other two methods generate). I am sure i am doing some Basic mistake but i cnt figure it.
    Attached Images Attached Images

  10. #10
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: circular reference within a cell using VBA

    As mentioned by MrShorty, I kinda have to use VBA since the bulk of the calculations are being carried out using vba.
    If the bulk of the calculations are already being carried out in VBA, why not move this calculation into VBA as well? Then you will avoid these kind of issues with the Excel circular reference, and you also avoid the overhead and performance penalties whenver Excel and VBA "talk" to each other. It should be a relatively simple loop
    Please Login or Register  to view this content.
    I was under the impression that VBA was not previously part of the spreadsheet, and that you were introducing VBA in an effort to resolve the problems with the circular reference. If VBA is already being used for other calculations, I would be inclined to just perform this calculation in VBA as well. In some ways, I prefer to perform iterative calculations in VBA because I can get better control over convergence criteria and more easily test if the algorithm is diverging.

  11. #11
    Registered User
    Join Date
    03-27-2017
    Location
    Germany
    MS-Off Ver
    office 365 pro plus
    Posts
    38

    Re: circular reference within a cell using VBA

    Hi there.

    Unfortunately I still have not been able to fix the issue. Since I would have an empty sheet before the macro runs and evey cell value
    will be filled by macro itself, I cnt seem to use the goal seek since the cell on which it has to apply goalseek does not orignially contain the formula.

    All i am looking for is a way for excel to solve this equation

    1/sqrt(f) = -2*log((k/d*3.7)+2.5/Re*sqrt(f))

    k and d in above equation are constant. Re is also a constant but i have a column of different Re values for which corresponsing (f) has to be calculated.
    The column containing Re is also not a fixed once but is generated dynamically by user by entering other variables. So the number of rows in the column containig
    Re keep on changing as per input .

  12. #12
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: circular reference within a cell using VBA

    I think a "divide and conquer" strategy will be helpful here.

    First observation: I thought that equation looked familiar: https://www.excelforum.com/excel-for...equations.html Even back then, a successive approximations algorithm like you are using was suggested and seemed to work just fine.

    IMO, the core of the problem is the root finding algorithm for that equation, which seems to work sometimes and not work others. The first thing I would do is get the root finding problem solved reliably and robustly. Here's a quick sheet I put together. I don't know what values for k, d, or Re are typical for this problem, but it seemed to converge reliably for the few values I tested. You will need to test it with values you expect and know the result for to see if it will reliably work for your applications. I would not worry about the other parts of the problem until I was satisfied that the root finding algorithm was working.

    the second part is how to fill in a table of different Re values. I thought that ChemistB's solution in the other thread was reasonable, but we can look at this part in more detail once you are satisfied with the root finding part.

    The final part of the problem is to use VBA to build the spreadsheet. I admit that I am not good at this kind of VBA programming, since I prefer to simply open an old spreadsheet and edit it or create a template and open that file rather than use VBA to build a spreadsheet. I don't think there is much value in worrying about this part of the problem until you have the other two parts figured out. Of course, after getting the spreadsheet put together without VBA, you may decide that you don't need VBA to build the spreadsheet, and this part will get skipped.

    Do you understand how this spreadsheet works? Test it for several different values of k, d, and Re. For which input values does it work, and which does it fail? Can you see how the "reset" column works to reset the loop?
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    03-27-2017
    Location
    Germany
    MS-Off Ver
    office 365 pro plus
    Posts
    38

    Re: circular reference within a cell using VBA

    Hi there. Thanks alot for the help. I finally fixed the Problem. Thanks to you guys.

    I have a small question though. My workbook contains many Sheets and except for first two Sheets, I want the macro to run on rest of the Sheets at the same time.

    I have named all of the Sheets but somehow it only works on a selected sheet. How would the macro look like in such Situation where the macro is to be run starting from 3rd sheet to the rest of the Sheets remaining. Thanks

  14. #14
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: circular reference within a cell using VBA

    I would probably nest the existing code inside of a For..Next loop (https://msdn.microsoft.com/en-us/lib.../gg251601.aspx ).
    1) Count the number of worksheets (if you have not already) using the .count property https://msdn.microsoft.com/en-us/lib.../gg251601.aspx
    2)
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    03-27-2017
    Location
    Germany
    MS-Off Ver
    office 365 pro plus
    Posts
    38

    Re: circular reference within a cell using VBA

    Thanks MrShorty. I got it fixed eventually. You guys have been really helpful in this regard. I just have the bigger Problems now on weather to use VBA or not. VBA makes my program run slow but if i dont use it, i got other Problems lol. I will post my Problem in a bit. If you have time, do take a look at my post.

    Thanks alot

+ 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. Stop Circular Reference when Cell Exceeds a Value
    By ferocity02 in forum Excel General
    Replies: 8
    Last Post: 11-17-2016, 10:20 PM
  2. [SOLVED] cannot resolve circular reference Cell AP35
    By hmr2662 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-16-2014, 01:50 PM
  3. Replies: 4
    Last Post: 03-07-2014, 06:57 PM
  4. Replies: 2
    Last Post: 02-23-2014, 06:06 PM
  5. Replies: 1
    Last Post: 08-21-2007, 07:22 PM
  6. Circular Cell Reference
    By karstens in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-15-2007, 09:42 PM
  7. circular error w/o cell reference
    By Remote Todd in forum Excel General
    Replies: 1
    Last Post: 08-11-2005, 08:05 AM

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