VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error? VB Noob - Call/Argument Error? VB Noob - Call/Argument Error? VB Noob - Call/Argument Error? VB Noob - Call/Argument Error? VB Noob - Call/Argument Error? VB Noob - Call/Argument Error? VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error? VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error?
Go Back  Xtreme Visual Basic Talk > > > VB Noob - Call/Argument Error?


Reply
 
Thread Tools Display Modes
  #1  
Old 02-19-2009, 08:13 PM
tjruler tjruler is offline
Newcomer
 
Join Date: Feb 2009
Posts: 1
Default VB Noob - Call/Argument Error?


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

'Count Iterations
OldCounter = Counter
Counter = OldCounter + 1

If Counter > 100000 Then
MsgBox "Iteration limit reached. Guess better next time, noob!", 48
ConvergeFlag = 1
End If

Loop

'Report Answers
Worksheets("sheet1").Range("D1").Value = ("Results")
Worksheets("sheet1").Range("D2").Value = ("Pf")
Worksheets("sheet1").Range("D3").Value = ("Pp")
Worksheets("sheet1").Range("D4").Value = ("Pi")
Worksheets("sheet1").Range("D5").Value = ("h")
Worksheets("sheet1").Range("D6").Value = ("y")
Worksheets("sheet1").Range("D7").Value = ("Ks")
Worksheets("sheet1").Range("D8").Value = ("Iterations")

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

End Sub
Reply With Quote
  #2  
Old 02-20-2009, 01:25 AM
Burningslash12's Avatar
Burningslash12 Burningslash12 is offline
Junior Contributor
 
Join Date: Sep 2008
Location: Singapore
Posts: 303
Default

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..."

Don't Click Me!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
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
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error? VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error? VB Noob - Call/Argument Error? VB Noob - Call/Argument Error? VB Noob - Call/Argument Error? VB Noob - Call/Argument Error? VB Noob - Call/Argument Error? VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error?
 
VB Noob - Call/Argument Error?
VB Noob - Call/Argument Error?
 
-->