ByRef target error on recursive function with a range of cells as input

sasori

New Member
Joined
Dec 7, 2016
Messages
3
CROSSPOSTED FROM HERE.

Hi everyone. I have a problem with my VBA code.

I have certain functions SUM_DIST and SUM_DISTN that require a range of cells as input. Both are working fine and in order. SUM_DIST has to be ran only once, then SUM_DISTN has to be run thrice, with SUM_DISTN requiring initial input from SUM_DIST, then the next two iterations would be from a previously calculated SUM_DISTN. I can manually get the results by plugging the functions in the spreadsheet.

My problem is when I want to go straight to getting the fourth iteration. What I did is to create another function SUM_DIST_COMP that will run the two functions - running the SUM_DIST function first, then run SUM_DISTN thrice using a for loop. While passing the contents of SUM_DIST_COMP to SUM_DIST was not a problem, I began to have problems in passing the results of SUM_DIST to SUM_DISTN. The code is as follows:

Code:
Option Explicit

Function SUM_DIST(xi As Variant, yi As Variant, zi As Variant, x As Double, y As Double, k As Double)
Dim arrx() As Variant: arrx = xi.Value
Dim arry() As Variant: arry = yi.Value
Dim arrz() As Variant: arrz = zi.Value
Dim di() As Variant
Dim ri() As Variant
Dim i As Integer
Dim i_count As Integer
Dim xn As Double
Dim yn As Double
Dim ri_total As Double


i = UBound(arrx) - LBound(arrx) + 1
xn = x
yn = y


ReDim di(1 To i, 1 To 1)
ReDim ri(1 To i, 1 To 1)


i_count = 1
For i_count = 1 To i
    di(i_count, 1) = (arrx(i_count, 1) - xn) ^ 2 + (arry(i_count, 1) - yn) ^ 2 + (arrz(i_count, 1) - k) ^ 2
Next i_count


For i_count = 1 To i
    ri(i_count, 1) = Sqr(di(i_count, 1))
Next i_count


ri_total = WorksheetFunction.Sum(ri)


SUM_DIST = ri_total
End Function


Function SUM_DISTN(xi As Variant, yi As Variant, zi As Variant, EG As Variant, x As Double, y As Double, k As Double)
Dim arrx() As Variant: arrx = xi.Value
Dim arry() As Variant: arry = yi.Value
Dim arrz() As Variant: arrz = zi.Value
Dim arrEG() As Variant: arrEG = EG.Value
Dim di() As Variant
Dim ri() As Variant
Dim ni() As Variant
Dim i As Integer
Dim i_count As Integer
Dim xn As Double
Dim yn As Double
Dim ri_total As Double


i = UBound(arrx) - LBound(arrx) + 1
xn = x
yn = y


ReDim di(1 To i, 1 To 1)
ReDim ri(1 To i, 1 To 1)
ReDim ni(1 To i, 1 To 1)


i_count = 1
For i_count = 1 To i
    di(i_count, 1) = (arrx(i_count, 1) - xn) ^ 2 + (arry(i_count, 1) - yn) ^ 2 + (arrz(i_count, 1) - k) ^ 2
Next i_count


For i_count = 1 To i
    ri(i_count, 1) = Sqr(di(i_count, 1))
Next i_count


For i_count = 1 To i
    ni(i_count, 1) = Abs((ri(i_count, 1) - arrEG(i_count, 1)) / arrEG(i_count, 1))
Next i_count


SUM_DISTN = WorksheetFunction.Sum(ni)
End Function


Function SUM_DIST_COMP(xi As Variant, yi As Variant, zi As Variant)
Dim arrx() As Variant: arrx = xi.Value
Dim arry() As Variant: arry = yi.Value
Dim arrz() As Variant: arrz = zi.Value
Dim EG_i As Variant
Dim EG_i_prev As Variant
Dim i_count As Integer
Dim i As Integer
Dim iter_SUM_DIST As Integer
Dim iter_SUM_DIST_limit As Integer
Dim xi_val As Double
Dim yi_val As Double


i_count = 1
iter_SUM_DIST = 3       'Repeat 3 times to get SUM_DIST_N4


i = UBound(arrx) - LBound(arrx) + 1
iter_SUM_DIST_limit = 5
ReDim EG_i(1 To i, 1 To 1)
ReDim EG_i_prev(1 To i, 1 To 1)
    
'Run initial iteration for SUM_DIST_N1
For i_count = 1 To i
    xi_val = xi(i_count, 1)
    yi_val = yi(i_count, 1)
    EG_i(i_count, 1) = SUM_DIST(arrx, arry, arrz, xi_val, yi_val, 0.1)
Next i_count


For iter_SUM_DIST = 1 To iter_SUM_DIST_limit
    EG_i_prev = EG_i
    For i_count = 1 To i
        xi_val = xi(i_count, 1)
        yi_val = yi(i_count, 1)
        EG_i(i_count, 1) = SUM_DISTN(xi, yi, zi, EG_i_prev, xi_val, yi_val, 0.1) '     <<<< THIS IS THE PROBLEMATIC LINE
    Next i_count
Next iter_SUM_DIST


SUM_DIST_COMP = EG_i
End Function


I have tried previous suggestions such as changing the input requirement for the receiving function SUM_DISTN's EG parameter to be Variant, but it still prints out #VALUE ! errors. An updated file is attached in the previous forum I crossposted from (BYREF_ERROR_NEW.xlsm).

Thanks in advance!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Pardon, I am unable to edit my post. The code posted above is incorrect; this is the unmodified code:

Code:
Option Explicit

Function SUM_DIST(xi As Variant, yi As Variant, zi As Variant, x As Double, y As Double, k As Double)
Dim arrx() As Variant: arrx = xi.Value
Dim arry() As Variant: arry = yi.Value
Dim arrz() As Variant: arrz = zi.Value
Dim di() As Variant
Dim ri() As Variant
Dim i As Integer
Dim i_count As Integer
Dim xn As Double
Dim yn As Double
Dim ri_total As Double


i = UBound(arrx) - LBound(arrx) + 1
xn = x
yn = y


ReDim di(1 To i, 1 To 1)
ReDim ri(1 To i, 1 To 1)


i_count = 1
For i_count = 1 To i
    di(i_count, 1) = (arrx(i_count, 1) - xn) ^ 2 + (arry(i_count, 1) - yn) ^ 2 + (arrz(i_count, 1) - k) ^ 2
Next i_count


For i_count = 1 To i
    ri(i_count, 1) = Sqr(di(i_count, 1))
Next i_count


ri_total = WorksheetFunction.Sum(ri)


SUM_DIST = ri_total
End Function


Function SUM_DISTN(xi As Variant, yi As Variant, zi As Variant, EG As Variant, x As Double, y As Double, k As Double)
Dim arrx() As Variant: arrx = xi.Value
Dim arry() As Variant: arry = yi.Value
Dim arrz() As Variant: arrz = zi.Value
Dim arrEG() As Variant: arrEG = EG.Value
Dim di() As Variant
Dim ri() As Variant
Dim ni() As Variant
Dim i As Integer
Dim i_count As Integer
Dim xn As Double
Dim yn As Double
Dim ri_total As Double


i = UBound(arrx) - LBound(arrx) + 1
xn = x
yn = y


ReDim di(1 To i, 1 To 1)
ReDim ri(1 To i, 1 To 1)
ReDim ni(1 To i, 1 To 1)


i_count = 1
For i_count = 1 To i
    di(i_count, 1) = (arrx(i_count, 1) - xn) ^ 2 + (arry(i_count, 1) - yn) ^ 2 + (arrz(i_count, 1) - k) ^ 2
Next i_count


For i_count = 1 To i
    ri(i_count, 1) = Sqr(di(i_count, 1))
Next i_count


For i_count = 1 To i
    ni(i_count, 1) = Abs((ri(i_count, 1) - arrEG(i_count, 1)) / arrEG(i_count, 1))
Next i_count


SUM_DISTN = WorksheetFunction.Sum(ni)
End Function


Function SUM_DIST_COMP(xi As Variant, yi As Variant, zi As Variant)
Dim arrx() As Variant: arrx = xi.Value
Dim arry() As Variant: arry = yi.Value
Dim arrz() As Variant: arrz = zi.Value
Dim EG_i As Variant
Dim EG_i_prev As Variant
Dim i_count As Integer
Dim i As Integer
Dim iter_SUM_DIST As Integer
Dim iter_SUM_DIST_limit As Integer
Dim xi_val As Double
Dim yi_val As Double


i_count = 1
iter_SUM_DIST = 3       'Repeat 3 times to get SUM_DIST_N4


i = UBound(arrx) - LBound(arrx) + 1
iter_SUM_DIST_limit = 5
ReDim EG_i(1 To i, 1 To 1)
ReDim EG_i_prev(1 To i, 1 To 1)
    
'Run initial iteration for SUM_DIST_N1
For i_count = 1 To i
    xi_val = xi(i_count, 1)
    yi_val = yi(i_count, 1)
    EG_i(i_count, 1) = SUM_DIST(xi, yi, zi, xi_val, yi_val, 0.1)
Next i_count


For iter_SUM_DIST = 1 To iter_SUM_DIST_limit
    EG_i_prev = EG_i
    For i_count = 1 To i
        xi_val = xi(i_count, 1)
        yi_val = yi(i_count, 1)
        'EG_i(i_count, 1) = SUM_DISTN(xi, yi, zi, EG_i_prev, xi_val, yi_val, 0.1) '     <<<< THIS IS THE PROBLEMATIC LINE
    Next i_count
Next iter_SUM_DIST


SUM_DIST_COMP = EG_i
End Function
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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