As this post will illustrate, I'm a total VB noob. I've been trying to write an interative solver for a system of equations, using a 'do while...." loop. The program seems to disagree that my variable inputs are valid, I'm currently assigning variables from a spreadsheet using the 'range' function.

Currently the debugger flags the bolded line with Error 5 - Invalid Call or Argument

Any input very apprecaited, I'm stumped.

The Code
----------------------------------------------------------------------

Option Explicit
Sub Iterate_PEMU_Parameters()
'Iterative solver to solve equilibria equations; specifically designed for PEMU films

'Assign Variables
Dim Kp As Double, Ki As Double, Cs As Double 'Constants
Dim F As Double, P As Double, I As Double 'where X = Px, representing the polymers in different forms
Dim F0 As Double, P0 As Double, I0 As Double 'old values of above
Dim C As Double 'Concentration of Ions
Dim y As Double 'Activity Coefficient
Dim x As Double, z As Double 'Changes in Concentration
Dim h As Double, k As Double, o As Double 'Film Thickness, Swelling Constant, old thickness (h0)
Dim ConvergeFlag As Boolean 'Convergence Flag
Dim Counter As Double, OldCounter As Double 'Iteration Counter

'Retrieve Initial Inputs
Kp = Worksheets("Sheet1").Range("B1").Value
Ki = Worksheets("Sheet1").Range("B2").Value
Cs = Worksheets("Sheet1").Range("B3").Value
F = Worksheets("Sheet1").Range("B4").Value
P = Worksheets("Sheet1").Range("B5").Value
I = Worksheets("Sheet1").Range("B6").Value

ConvergeFlag = 0
Counter = 0

'Set h to 1
k = (F + I) / P
h = 1
o = 1

'Iterative Loop
Do While ConvergeFlag = 0

'Equations for one iteration
y = 10 ^ (-0.51 * Sqr(F + Cs))

x = (-(4 * y * F + P) - Sqr((4 * y * F + Kp) ^ 2 - 4 * (4 * y) * (y * (F ^ 2) - Kp * P))) / (2 * 4 * y)
If x < 0 Then
x = (-(4 * y * F + P) - Sqr((4 * y * F + Kp) ^ 2 - 4 * (4 * y) * (y * (F ^ 2) - Kp * P))) / (2 * 4 * y)
Else
End If

z = (-(-y * Cs - y * F - Ki) - Sqr((-y * Cs - y * F - Ki) ^ 2 - 4 * (y) * (y * F * Cs - Ki * I))) / (2 * y)
If z < 0 Then
z = (-(-y * Cs - y * F - Ki) + Sqr((-y * Cs - y * F - Ki) ^ 2 - 4 * (y) * (y * F * Cs - Ki * I))) / (2 * y)
Else
End If

'Store New Values
F = F + 2 * x - z
P = P - x
I = I + z

'Calculate new Thickness
o = h
h = k * (2 * P) / (F + I)

'Correct for Concentrations
F0 = F
F = F0 * (h / o)
P0 = P
P = P0 * (h / o)
I0 = I
I = I0 * (h / o)

'Convergence Test
If x < 0.0001 And y < 0.0001 Then ConvergeFlag = 1

Worksheets("sheet1").Range("E2").Value = F
Worksheets("sheet1").Range("E3").Value = P
Worksheets("sheet1").Range("E4").Value = I
Worksheets("sheet1").Range("E5").Value = h
Worksheets("sheet1").Range("E6").Value = y
Worksheets("sheet1").Range("E7").Value = k
Worksheets("sheet1").Range("E8").Value = Counter

Hi! I believe you're running this on VBA right? I think you posted in the wrong forum, this is for .NET.

Anyway, i copied and pasted your codes in a workbook, and just punched in a value of 1 for Range(B1:B6). I got the same error, but in a different line.

This line:

Code:

'Equations for one iteration
y = 10 ^ (-0.51 * Sqr(F + Cs))

Looking at this, the only function you are calling is SQR. Debugging will reveal that the argument of SQR becomes negative during the iteration, and this is the problem. SQR cannot accept values less than 0.

Here is from the help file of excel VBA:

Quote:

Sqr Function

Returns a Double specifying the square root of a number.

Syntax

Sqr(number)

The required number argument is a Double or any valid numeric expression greater than or equal to zero.

Hope this helps.

__________________
Burningslash12
"We may rise and fall, but in the end, we'll meet our fate together..."

The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET. subscribe