Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact


Runtime Error 5 - Call/Argument? VB noob!


Runtime Error 5 - Call/Argument? VB noob!

Author
Message
tjruler
tjruler
Forum God
Forum God (371 reputation)Forum God (371 reputation)Forum God (371 reputation)Forum God (371 reputation)Forum God (371 reputation)Forum God (371 reputation)Forum God (371 reputation)Forum God (371 reputation)Forum God (371 reputation)

Group: Forum Members
Posts: 1, Visits: 2
As this post will illustrate, I'm a total VBA 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

GO


Similar Topics


Reading This Topic


Login
Existing Account
Email Address:


Password:


Social Logins

Select a Forum....

















A1VBCode Forums


Search