无法从Excel工作表单元格调用VBA函数

当试图从Excel工作表单元格中使用自定义的VBA函数时,出现错误:“您键入的公式包含错误”

但是,当我从VBA调用相同的函数时,它工作正常。

VBAfunction:

Public Function R1C1_value(RowIndex As Integer, ColIndex As Integer, Optional WorkSheetName As String) As Variant Dim ws As WorkSheet If Trim(WorkSheetName) = "" Then Set ws = ActiveSheet Else Set ws = ActiveWorkbook.Worksheets(WorkSheetName) End If R1C1_value = ws.Cells(RowIndex, ColIndex).Value End Function 

VBA函数调用:

 Sub R1C1_value_test() Dim res res = R1C1_value(1, 9, "CVP") MsgBox "res: " & res End Sub 

Excel函数调用

 =R1C1_value(1,9) 

即使在VBA中接受,函数名称在Excel中也不是有效的名称。 如果您将其更改为xR1C1_value,您的function应该工作。