如何在带有函数的单元格中编写公式?(复制)

时间:2023-01-21 14:57:20

This question already has an answer here:

这个问题已经有了答案:

I would like write a formula in a specific cells with a function.

我想用一个函数在一个特定的单元格中写出一个公式。

I have written this, but it doesn't work and I don't understand why :

我写了这个,但它不起作用,我不明白为什么:

Function formulaCell(x, y)

ActiveSheet.Cells(x, y).FormulaR1C1 = "=IF(R[-1]C=0,"""",R[-1]C)"

End Function

Excel return #VALEUR

Excel #返回的数值

Thanks in advance

谢谢提前

1 个解决方案

#1


1  

Your function will work just fine if the function is not called from a cell.

For example, here it is call from a Sub:

如果不从单元格调用函数,则函数将正常工作。例如,在这里它是一个下标的调用:

Sub MAIN()
    Dim msg As String
    msg = formulaCell(3, 3)
    MsgBox msg
End Sub

Function formulaCell(x As Long, y As Long) As String
    ActiveSheet.Cells(x, y).FormulaR1C1 = "=IF(R[-1]C=0,"""",R[-1]C)"
    formulaCell = "Mission Accomplished!!"
End Function

A UDF() in a cell can only return a value to that cell. A UDF() in a Sub can do much more!

单元格中的UDF()只能向该单元格返回一个值。潜艇上的UDF()可以做得更多!

#1


1  

Your function will work just fine if the function is not called from a cell.

For example, here it is call from a Sub:

如果不从单元格调用函数,则函数将正常工作。例如,在这里它是一个下标的调用:

Sub MAIN()
    Dim msg As String
    msg = formulaCell(3, 3)
    MsgBox msg
End Sub

Function formulaCell(x As Long, y As Long) As String
    ActiveSheet.Cells(x, y).FormulaR1C1 = "=IF(R[-1]C=0,"""",R[-1]C)"
    formulaCell = "Mission Accomplished!!"
End Function

A UDF() in a cell can only return a value to that cell. A UDF() in a Sub can do much more!

单元格中的UDF()只能向该单元格返回一个值。潜艇上的UDF()可以做得更多!