As a newbie to VBA, I am trying to build a procedure that loops through a set of rows -- each containing numerical values for several country-specific parameters -- and for each row apply the Newton-Raphson method to find the value of "H" that solves an equation for that country. That method involves starting with a "guess" of H, computing two functions: Diff (the difference between the two sides of the equation with the current value of H) and Diff_prime (the slope of the equation at the current value of H), then computing a new value of H as Hnew = Hcurrent - Diff(Hcurrent)/Diff_prime(Hcurrent); and repeating until Diff is close enough to 0.
By liberally borrowing from others' examples, I cobbled together a sub-procedure that works, but it's ugly: I can can declare H as a Range and then pass the value of H to the functions Diff(H) and Diff_prime(H), one row at a time; however, the only way I've been able to get the other parameter values into the functions is in the form of Offsets relative to the cell containing H. As you can see from the code below, that makes the functions VERY hard to understand.
But whenever I try to replace one of those offsets with an argument that points directly at a parameter other than H, I get a Type Mismatch error.
Can anyone suggest a way to write such functions so they avoid this problem? Ideally, I'd like to be writing Diff as (something like)
Diff(H as Range, theta as Range, gamma as Range, delta as Range, mu as Range, Povline as Range) and then, for each row, solving H as a function of the values of theta, gamma, delta, etc in that row.
From looking at a Watch window, I can see that VBA understands those parameters as Ranges (period) rather than looking at the values of the parameters in the row containing the information for the country the function is working on.
Can anyone suggest a way to do this? Thanks a million!
--------------------
![]()
Please Login or Register to view this content.
Bookmarks