简体   繁体   中英

Unable to call VBA function from Excel Worksheet Cell

While trying to use a custom made VBA function from an excel worksheet cell, I get the error: "The formula you typed contains an error"

However, when I call the same function from VBA, it works alright.

VBA Function:

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 Function Call:

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

Excel Function call

=R1C1_value(1,9)

The function name is not a Valid name in Excel, even though it is accepted in VBA. If you change it to xR1C1_value, your function should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM