Here is the macro that I have
Option Explicit
Function AutoFormula(blocks As Range, target As Range)
Dim blockedArr() As String
Dim cellValue As String
Dim ret As String
Dim i As Integer
'sprawdzenie zakresu
If (blocks.Cells.Count > 1) Then
AutoFormula = "Tylko 1 komórka jako parametr!"
Exit Function
End If
'wczytanie komorki
cellValue = blocks.Cells(1, 1).Value
'split
blockedArr = Split(cellValue, ",")
ret = "=WYSZUKAJ.PIONOWO(E7;$E$2:$G$5;3;FAŁSZ)"
For i = LBound(blockedArr, 1) To UBound(blockedArr, 1)
ret = ret + "+SUMA.JEŻELI(A7:A1000;" + blockedArr(i) + ";G7:G1000)"
Next i
target.Cells(1, 1).FormulaLocal = ret
AutoFormula = 1
End Function
Sub auto()
Call AutoFormula(Worksheets("reorganizacja").Range("D7"), Worksheets("reorganizacja").Range("G7"))
End Sub
When I run it using the auto() sub it works fine and produces a desired result. However, if I call it as a formula in excell cell like =AutoFormula(D7;G7) it doesn't work. In the Excell cell I get the #ARG error. When I debug the macro it goes properly until target.Cells(1, 1).FormulaLocal = ret line and when I step over it breaks the function immediately without completing it.
Any ideas?