Share via

Setting the cell name programmatically as a while loop condition when using Excel Python xl() function

Anonymous
2024-12-06T01:20:29+00:00

When I hard code this,

a = xl("A1")

Python under Excel works fine. When I code this instead,

c = "A1"

a = xl(c)

I get this error

KeyError: 'A1'

Frustrating.

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2024-12-06T13:52:29+00:00

This is by design in custom xl() function. Hope Microsoft will add more custom function for Python Excel.

PY function - Microsoft Support

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2025-01-06T18:54:38+00:00

    I think this is related to the way xl() and Python integration has been implemented. It appears there is some sort of preprocessing of the xl() function, i.e. all the xl() function calls are resolved before the rest of the Python code is executed. This implies the Python code must be "read-only' and as such cannot update Excel cell contents due to the preprocessing design constraint. Two gotcha's for the price of one design snafu,

    1. Cannot use variables as arguments to the xl() function, and,
    2. Python running withing Excel cannot update values in Excel cells.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2024-12-08T18:18:04+00:00

    I have to say, I struggle a bit with this. Seems like a string should be a string. I have several tables and would like to load them by using variable names. I also have a bunch of assumptions and would like to load them in a loop. If a string is not a string, then these approaches, like the OP's, just won't work. It also introduces a required level of hard-coding, which is bad in Excel and worse in programming, IMHO.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-12-06T13:23:39+00:00

    Thank you, when will this be fixed?

    Also, I couldn't find the refence documentation you've so kindly listed. Presumably it was placed somewhere obvious by the Vogon planning department of Alpha Centauri. Nonetheless a link to it would be awesome.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2024-12-06T05:02:49+00:00

    This is caused by xl() function not accept variable.

    Was this answer helpful?

    0 comments No comments