Results 1 to 8 of 8

Need to loop through a set of functions and access the changing variable in the functions

Threaded View

  1. #1
    Registered User
    Join Date
    06-13-2017
    Location
    united states
    MS-Off Ver
    2016
    Posts
    11

    Need to loop through a set of functions and access the changing variable in the functions

    Hey, I've been having an issue. I need to set up a Newton Raphson method for approximating an answer to a complicated equation, line after line, for many conditions.

    So for those who just want to skim this, my problem is that I need to set up a for loop inside of a sub that calls out functions inside said for loop, but I need the functions to have access to and change with the same changing variable set by the for loop.

    Here's some context for the problem: I need to solve one cell based off of another similar to the way goalseek or solver would function, and loop down like 50 rows, referencing a table of values. The purpose of setting up this function using the Newton Raphson method has been that I am solving a complex math problem that has weird answers and I need to arrive at a real solution, and I need to do so more quickly than the solver or goalseek functions have been able to do for me. Which works! The newton raphson method is WAY faster for one line in my case, so that's not the issue.

    So here's the part I need help with: I have one line/row of my spreadsheet completely solved and it solves the equation I need lightning fast. To do so, I have 2 functions set up.
    My first function, f(x), is the equation I have simplified my problem to, and the second function, df(x), is its derivative. I use these to perform a taylor-series like iterative solution using a 3rd function, in which I call out those first two functions. Another subroutine calls for that 3rd function, and then outputs the value that the 3rd function spits out into a cell. All of this works phenomenally, for one line. My original plan was to solve for one line, and then put a big for loop around the whole procedure, referencing the changing variable in order to change the cells that the functions reference, and solve for many solutions in a vertical column that lines up with the rest of my data. Please let me know if that's not clear enough, I'll be happy to elaborate.

    Setting up a for loop inside of each cell obviously won't work, and the variable I loop through, "j", can't be referenced within the other functions if I call it out within the sub, where it needs to be. I want to call out, for example,
    b = Cells(j, "CJ") within the function that the sub calls for. I want to have the sub be a big for loop that changes j... and reference j within the functions as it changes.

    I HAVE GOT AROUND my problem by making a for loop that inside my sub fills up a column in the spreadsheet with 1's, and in my functions I set j equal to a command that counts the number of cells filled in the column range, but since it has to output cells and count rows it takes too long for the size of data I need it for (like 50 lines.) Is there ANY way to reference a variable, WITHIN A CHANGING FOR LOOP, inside of a function that you call out? Will global variables work? (I've tried global variables but can't set one up that changes where I need it to.) PLEASE help me if you can, I'm going insane trying to find an answer, all I've found on most threads is basic for loop help and referencing things within subs, but nothing that quite helps with my super specific problem. Any help would be appreciated, this is my first time posting in a thread so any thread advice would also be appreciated. Thanks in advance!

    I've provided a workbook set up the same way as my code. Thanks again, please let me know if I can clarify further!
    Attached Files Attached Files
    Last edited by jacktbg; 06-13-2017 at 01:49 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 9
    Last Post: 06-13-2017, 01:41 PM
  2. [SOLVED] How to use if and loop functions
    By gogz2005 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-15-2014, 08:30 AM
  3. Access add in functions to VBA
    By OCONUS in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-09-2012, 10:12 AM
  4. How to use my Access VBA functions in Excel
    By new2VBAexcel in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-22-2010, 10:28 AM
  5. Use for loop with functions
    By Rodas in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-03-2009, 02:29 PM
  6. Trying to call access functions in excel
    By DKY in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-19-2008, 03:38 PM
  7. [SOLVED] Not able to access certain Excel functions
    By LauraJ3 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-01-2006, 11:40 PM

Tags for this Thread

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