I have got a very simple function in VBA in which I work on a Range with multiple columns and rows. First I wanted to sort that Range over first column (filled with dates):
Public Function getCOF(data_zawarcia_tf As Date, waluta_tf As String, czas_trwania_tf As Double, _
table_to_search As Range, output As Range) As Double
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
table_to_search = table_to_search.Sort(Key1:=Range("A1"), _
Order1:=xlAscending)
MsgBox "h"
MsgBox table_to_search(1, 1)
czas_trwania_tf = -Int(-czas_trwania_tf / 12) * 12
For i = lastRow To 1 Step -1
If table_to_search(i, 1) < data_zawarcia_tf And table_to_search(i, 2) = "S" And _
waluta_tf = table_to_search(i, 3) And czas_trwania_tf = table_to_search(i, 4) Then
getCOF = output(i)
Exit For
End If
Next i
End Function
but when I do this this way, the function doesn't even call MsgBox (can you tell me why I don't even get an error?). The function doesn't compile and i get #ARG! in my cell.
I also tried to do:
Public Function getCOF(data_zawarcia_tf As Date, waluta_tf As String, czas_trwania_tf As Double, _
table_to_search As Range, output As Range) As Double
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
table_to_search.Sort Key1:=Range("A1"), _
Order1:=xlAscending
MsgBox "h"
MsgBox table_to_search(1, 1)
czas_trwania_tf = -Int(-czas_trwania_tf / 12) * 12
For i = lastRow To 1 Step -1
If table_to_search(i, 1) < data_zawarcia_tf And table_to_search(i, 2) = "S" And _
waluta_tf = table_to_search(i, 3) And czas_trwania_tf = table_to_search(i, 4) Then
getCOF = output(i)
Exit For
End If
Next i
End Function
and this time the function do call MsgBox, but MsgBox table_to_search(1, 1) with ascending sort is same as with descending.
How can I sort my Range object?
Also is there a way to work on Range as on python's/R's dataframe? That is something like
get_value = my_range(my_range(all_rows, 1) = "12.02.2005", my_range(all_rows, 2) = "Los Angeles")(3)
that means get_value would be value from 3rd column where 1st and 2nd columns are "12.02.2005" and "Los Angeles"