I'm trying to write a UDF that calculates the slope between 2 variable ranges. But it stucks when I declare the ranges
Whats wrong with this?


Function varc(ticker)

Dim rang1, rang2 As Range

Set cart = Sheets("CARTEIRA")
Set ret = Sheets("Retorno")
'ticker = "ITUB4"

uLinha1 = cart.Range("A2").End(xlDown).Row
uLinha2 = ret.Range("A1").End(xlDown).Row

a = Application.WorksheetFunction.Match(ticker, ret.Range("1:1"), 0)

rang1 = cart.Range(Cells(uLinha1 - 20, 31), Cells(uLinha1, 31))   <=======
rang2 = ret.Range(Cells(uLinha2 - 20, a), Cells(uLinha2, a)) <========

slp = Application.WorksheetFunction.Slope(rang2, rang1)
partc = ActiveCell.Offset(0, -4)

varc = slp * partc

End Function
Thanks!