I have an User-Defined function in Excel Vba. I want to change the values of the cells in the range passed through as parameter using this function.
=ChangeCellValues(J18:J19)
When I call the function in formula editor as above in excel, the execution stops in the line where the assignment is performed and I get an value error in runtime. I have tried many syntactical options but no way.
However when I call the function in a main sub procedure, it works somehow and changes the values of the cells
Function ChangeCellValues(myrange As range) As Double
Dim i As Integer
For i = 1 To myrange.Rows.count
'None of them works.
mycell.Cells(i, 1).Value = 1
mycell.Cells.Item(i, 1).Value = 1
mycell.Item(i, 1).Value = 1
Next i
ChangeCellValues = 0
End Function
Sub Main()
Debug.Print ChangeCellValues(range("J17:J18")) 'This works.
range("J17").Value = 10 'This works also.
End Sub
In essence, the question is how can I change the values of some cells in a worksheet passed through as argument in Excel Vba function?