Excel VBA: Workbook-scoped, worksheet dependent named formula/named range (result changes depending on the active worksheet)

前端 未结 2 1400
一个人的身影
一个人的身影 2020-12-09 05:43

EDIT: Title changed for clarity.

Quick summary: I want to know if the behavior of workbook-scoped, worksheet dependent named formulas (which I will

相关标签:
2条回答
  • 2020-12-09 05:57

    As for the documentation, see Evaluating Names and Other Worksheet Formula Expressions

    • =A1 refers to cell A1 on the current sheet
    • =!A1 refers to cell A1 on the active sheet

    in conjunction with Worksheet References

    • Current refers to what Excel is recalculating ...
    • Active refers to what the user is viewing ...

    This is what Charles Williams demonstrated. As for your use case, I'd recommend user defined functions, say in VBA.

    0 讨论(0)
  • 2020-12-09 06:20

    You need to be careful using !References in Names:
    Names with refers-to starting with =! may give incorrect results when calculation is called from VBA. They are calculated as though they always refer to the active worksheet rather than the sheet that they are being used on:

    put 1 in cell A1 of Sheet1
    a) put =CellA1 in A2 of Sheet1, where CellA1 is defined as =!$A$1
    b) switch to manual calculation
    c) activate Sheet2
    d) run this VBA code

    Sub Testing()
    Worksheets("Sheet1").Range("a1") = 8
    Application.Calculate
    End Sub
    

    e) Now switch back to Sheet1 and you will see that Sheet1!A2 contains whatever was in Sheet2!A1

    Using indirect() avoids this problem
    Our Name Manager addin detects and warns about using this kind of syntax.

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