Range

Hello,

I'm developing a macro to do some calculations on a worksheet, but I always get a problem when I pass a Range as an argument to my function. I need to pass the range as argument.

For example:

Sub Name()

Dim A As Range

Set A = Worksheets("Sheet1").Range("CD4")

MyFunction(A)

End Sub

Function MyFunction (ValueDoRange As Range)

etc...

End Function

Thanks



Answer this question

Range

  • Gustav OK

    Hi,

    You don't say exactly what error you get but these two things may get your further along.

    Name is a bad choice for a routine name. It will compile but when you try to run it gives.

    Compile error:
    Expected: expression

    The other problem is the use of brackets around the passing argument without assigning the return value to a variable.

    Either lose the bracets

    MyFunction A

    Or assign return value

    MyResult =
    MyFunction(A)




  • braz

    Thank you Andy.
  • vanderkerkoff

    It is because you are trying to treat an array as a range.

    In the function InterpMatriz_lin you have declared U as an array,

    Dim U(1 To 100)

    You then pass U to the function InterpVector like this,

    InterpMatriz_lin = InterpVector(U, B, y) '"This is the problem"

    The signature of the function is,

    Function InterpVector(A, B, x) '"This function works well"

    And inside the function you reference A as if it where a range, which it is not.

    N = A.Rows.Count ' this is not possible

    So assuming you still want to know how many rows use this instead. It will return 100, which is the size of the array.


  • Benin

    Excuse me for my bad english

    I Have this code:

    Function InterpMatriz_lin(Rango, x, y)

    Dim T(1 To 100)
    Dim R(1 To 100, 1)

    Dim S(1 To 100)
    Dim A(1 To 100, 1)
    Dim U(1 To 100)
    Dim B(1 To 100)

    NumFilas = Rango.Rows.Count
    NumCols = Rango.Columns.Count

    For i = 2 To NumFilas
    T(i - 1) = Rango(i, 1)
    Next i

    For i = 2 To NumCols
    R(i - 1, 1) = Rango(1, i)
    Next i

    For i = 2 To NumCols
    For j = 2 To NumFilas
    S(j - 1) = Rango(j, i)
    Next j
    A(i - 1, 1) = InterpVector(T, S, x) "This is the problem"
    Next i

    For i = 1 To (NumCols - 1)
    B(i) = A(i, 1)
    U(i) = R(i, 1)
    Next i

    InterpMatriz_lin = InterpVector(U, B, y) "This is the problem"


    End Function "This funtion does not work well"

    Function InterpVector(A, B, x) "This function works well"

    Dim AA(100) 'Se limita el tamano de la serie a 100
    Dim BB(100)

    N = A.Rows.Count


    Amax = -9E+99
    For i = 1 To N
    If A(i) > Amax Then Amax = A(i): Pmax = i
    Next i

    Amin = 9E+99
    For i = 1 To N
    If A(i) < Amin Then Amin = A(i): Pmin = i
    Next i

    For i = 1 To N
    If Pmin > Pmax Then AA(i) = A(N + 1 - i): BB(i) = B(N + 1 - i)
    If Pmin < Pmax Then AA(i) = A(i): BB(i) = B(i)
    Next i


    If x < AA(1) Then x1 = AA(1): y1 = BB(1): x2 = AA(2): y2 = BB(2)

    If x > AA(N) Then x1 = AA(N - 1): y1 = BB(N - 1): x2 = AA(N): y2 = BB(N)
    POS = 1
    For i = 2 To N
    If x >= AA(i) Then POS = POS + 1
    Next i
    If x >= AA(1) And x <= AA(N) Then x1 = AA(POS): y1 = BB(POS): x2 = AA(POS + 1): y2 = BB(POS + 1)

    InterpVector = y1 + (y2 - y1) * (x - x1) / (x2 - x1)

    End Function


  • Range