'invalid control variable reference' error

JenkinsMeechelle

New Member
Joined
Feb 20, 2015
Messages
3
Hi all,
I am trying to copy an example from a textbook so that i can edit it for my own use and i have typed out the Macro they apparently used. i Have checked a few time and can't see the error but i keep getting a 'invalid control variable reference' error and can't get any further. I would appreciate any help on fixing this issue! The code is copied directly from VBA below.
Code:
DefDbl A-H, K-L, P-Z
DefLng I-J, M-O
Sub Fig8_1()
Dim aold(256), anew(256), Vz(256)
Dim A(256), B(256), C(256), D(256)


ain = 1
Da = 0.000000005
L = 2
R = 0.01
U = 0.01
k = 0.005
Itotal = 2


For jj = 1 To 7 'this outer loop varies Itotal to check convergence
    Itotal = 2 * Itotal
    If Itotal = 4 Then Jtotal = 2
    If Itotal = 8 Then Jtotal = 4
    If Itotal > 8 Then Jtotal = 8 * Jtotal
    dr = R / Itotal
    dz = L / Jtotal


'Set constants in Eq 8.26
    A(0) = 4 * Da / dr ^ 2 * dr / 2 / U
    B(0) = -4 * Da / dr ^ 2 * dz / 2 / U
    D(0) = -k * dz / 2 / U
    aold(0) = 1


'Set constants in Eq 8.25
   For i = 1 To Itotal - 1
    Vz(i) = 2 * U * (1 - (i * dr) ^ 2 / R ^ 2)
    A(i) = Da * (1 / (2 * dr ^ 2 * i) + 1 / dr ^ 2) * dz / Vz(i)
    B(i) = Da * (-2 / dr ^ 2) * dz / Vz(i)
    C(i) = Da * (-1 / (2 * dr ^ 2 * i) + 1 / dr ^ 2) * dz / Vz(i)
    D(i) = -k * dz / Vz(i)
    aold(i) = 1
   Next


'Set the initial conditions
   For i = 0 To Itotal
    aold(i) = ain
   Next


'March down the tube
   For j = 1 To Jtotal
    anew(0) = A(0) * aold(1) + (1 + B(0)) * aold(0) + D(0) * aold(0)
   
'This is the sideways shuffle
   For i = 1 To Itotal - 1
    x = A(i) * aold(i + 1) + (1 + B(i)) * aold(i)
    anew(i) = x + C(i) * aold(i - 1) + D(i) * aold(i)
   Next j
    Next i


'Apply the wall boundary condition, Eq 8.27
   anew(Itotal) = 4 * anew(Itotal - 1) / 3 - anew(Itotal - 2) / 3
   
'March a step forward
   For i = 0 To Itotal
    aold(i) = anew(i)
   Next i
'Calculate the mixing cup average
    F = 0
    Q = 0
   For i = 1 To Itotal - 1
    F = F + 2 * dr * i * Vz(i) * anew(i)
    Q = Q + 2 * dr * i * Vz(i)
   Next i
   y = F / Q
'Output results for this mesh size
    Range("A" & CStr(jj)).Select
    ActiveCell.FormulaR1C1 = Itotal
    Range("B" & CStr(jj)).Select
    ActiveCell.FormulaR1C1 = Jtotal
    Range("C" & CStr(jj)).Select
    ActiveCell.FormulaR1C1 = y
    
    Next jj
        
End Sub
 
Last edited by a moderator:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Swap these around.
Code:
Next j
Next i
 
Upvote 0
That's where the code I'm referring to comes from.

It should be this.
Code:
Next i
Next j
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top