0

i installed the Add-on PyXll and all is set to work fine.

i defined a function for fetch an Array in excel which under the function and i want to re-use this function on many arrays, so i would like to use the Reference L1C1 for be relative to the function.

my function is on A1 when i do :

xl = xl_app()
specific_range = xl.ActiveSheet.Range.("A2:B5")

works well!

but if i do :

xl = xl_app()
specific_range = xl.ActiveSheet.Range("L(1)C(0):L(4)C(1)")

or

specific_range = xl.ActiveSheet.Range("L1C0:L4C1")

not work ! ##com_error: (-2147352567, 'Une exception s’est produite.', (0, None, None, None, 0, -2146827284), None)

how to configure for having the reference L1C1 ? or is it another way to get my result ?

Thanks a lot

maxashtar
  • 21
  • 5

1 Answers1

0

The Excel API exposed to Python is the same as the one you might have used in VBA. In general, if you can figure out how to do what you want in VBA then you can translate it to Python. The follow page from the PyXLL docs should help further: https://www.pyxll.com/docs/userguide/vba.html.

To get a range relative to the current function you can use the Application.Caller property: https://learn.microsoft.com/en-us/office/vba/api/Excel.Application.Caller

Once you have the calling range you can use the Offset property to get adjacent cells: https://learn.microsoft.com/en-us/office/vba/api/Excel.Range.Offset

For example,

from pyxll import xl_app

xl = xl_app()
caller = xl.Caller
specific_range = caller.GetOffset(1, 2)

You can construct a Range from the top left and bottom right cells, eg

xl = xl_app()
top_left = xl.Caller
bottom_right = caller.GetOffset(1, 2)
specific_range = xl.Range(top_left, bottom_right)

Another thing to look at is the "automation_example" function included in the "automation.py" examples in the PYXLL download. This example uses a similar but slightly different way to get a range below the calling cell, and uses PyXLL's async_call function to schedule a function to write to that range after Excel has finished its calculations.

You can instead pass the range into your function. That is a more usual way of passing data like this into a function. The following docs should help

The advantage of doing it this way is that you don't need to get the range in code, you can just pass it in to the function, and each time a value changes in that range then your function would be called.

For example, you could do

from pyxll import xl_app

@xl_func("var[][]: str")
def test(values):
    return str(values)

# In Excel...
# A1: =test(A2:B6)
# E1: =test(E2:F6) 
Tony Roberts
  • 387
  • 1
  • 5