#Value error while accessing user defined function in VBA

前端 未结 1 1788
太阳男子
太阳男子 2020-12-07 06:09

Code works fine when I access function from VBA however when I call the same function in excel cell (postalcode(\"23.0776120,72.6538530\"), I get #Value error.

1条回答
  •  春和景丽
    2020-12-07 06:39

    It's a documented limitation of User-Defined Functions that you cannot generally operate on or manipulate range/worksheet objects within a UDF called from the worksheet. While you can do most value/property queries, you can't change the environment:

    A user-defined function called by a formula in a worksheet cell cannot change the environment of Microsoft Excel. This means that such a function cannot do any of the following:

    • Insert, delete, or format cells on the spreadsheet.
    • Change another cell's value.
    • Move, rename, delete, or add sheets to a workbook.
    • Change any of the environment options, such as calculation mode or screen views.
    • Add names to a workbook. Set properties or execute most methods.

    I suspect that stepping through this code using F8 key in the VBE will identify the error, probably Worksheets("Sheet1").Cells(1,6).Value.

    The basic rationale behind this limitation is to prevent infinite loops/circular references.

    There are ways to circumvent this limitation.

    0 讨论(0)
提交回复
热议问题