Forum Discussion
Python in Excel iteration of sheets
- Jan 18, 2024
Python in Excel doesn't work with such kind of indirect references, i.e.
a = "Sheet2!A1:A2" xl(a)
returns the same error. xl() accepts direct names of the Excel objects.
Thank you for a response.
I would agree with you if it weren't for the fact that it works after another direct loading of those specific cells line pointing to the sheet.
for i in range(2,4):
value = f"Sheet{i}!A1:A2"
print(xl(value))
Does not work for Sheet2, however,
xl("Sheet2!A1:A2")
for i in range(2,4):
value = f"Sheet{i}!A1:A2"
print(xl(value))
For Sheet2 does work
xl("Sheet2!A1:A3")
for i in range(2,4):
value = f"Sheet{i}!A1:A2"
print(xl(value))
Does not work for Sheet2
Very strange.
Yes I am having the same problem - getting a "key error" when trying to use xl() with a variable. I would love it if someone had a way to make this work. Or if it could be added in a future update. I have also noticed that it sort of works if you hardcode the cells elsewhere the load them using xl() which is very strange but not super helpful.