Error in passing data from a Function to a Subroutine

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
358
Office Version
  1. 2003 or older
Platform
  1. Windows
I have a function from which I am passing data to a subroutine and then populating the output. When I am using x, y as is, I am getting the output, but when I use arr1, arr2 instead I do not get an output. The code is:

Code:
Option Explicit
Option Base 1
Public Function Func1(ByVal x As Range, ByVal y As Range) As Variant


    Dim n As Long, i As Long, j As Long, k As Long, c As Long, n1 As Long, n2 As Long


    n = Application.WorksheetFunction.Max(Application.WorksheetFunction.Count(x), Application.WorksheetFunction.Count(y))
MsgBox n
    Dim arr1() As Double, arr2() As Double
    ReDim arr1(1 To n)
    ReDim arr2(1 To n)


    j = 0
    k = 1
    For i = 1 To n
        If Application.WorksheetFunction.IsNumber(x(i)) = False Or Application.WorksheetFunction.IsNumber(y(i)) = False Then
            j = j + 1
        Else
            arr1(k) = x.Cells(i, 1).Value2
            arr2(k) = y.Cells(i, 1).Value2
            k = k + 1
        End If
    Next


    ReDim Preserve arr1(1 To n - j)
    ReDim Preserve arr2(1 To n - j)


    Dim m As Double
    Dim d As Double


Call Proc1(x, y, m, d) ' This is working


Call Proc1(arr1, arr2, m, d) ' This is not working
    
    Func1 = (m / d)


End Function


Private Sub Proc1(ByVal x As Variant, ByVal y As Variant, ByRef m As Double, ByRef d As Double)


    Dim n As Long
    n = (x.Rows.Count + y.Rows.Count) / 2
    Dim c As Double    ' C(n,2)
    c = n * (n - 1) / 2


    d = Sqr((c) * (c - n / 2))
    m = Sqr(d + n)


End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You can't pass an array ByVal, it has to be ByRef. Also, an array doesn't have properties like Rows so your sub wouldn't work.

Edit: you can ignore the part about ByVal - I hadn't spotted you were using Variants.
 
Last edited:
Upvote 0
So, how can I convert the array to a range which can then be passed?
 
Upvote 0
Only by putting the array into cells somewhere. Or just rewrite the sub to take an array.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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