'Xlwings take value from defined names
I have an excel with a defined name, cell A1 has assigned name "myName" is there a way with xlwings to take its content from its name rather than on its coordinates?
It should be the same of
title, coord = next(wb.defined_names['myName'].destinations)
content = wb[title][coord].value
in openpyxls
Solution 1:[1]
Sure, just do:
import xlwings as xw
xw.sheets[0].range('myName').value
Solution 2:[2]
import xlwings as xw
content = xw.Range('myName').value
This avoids the positional reference to the sheet so that you can copy/paste your named range from one sheet to another as you build.
Solution 3:[3]
Using xlwings, very similarly to VBA, you could use the following
range_object = wb.names("named_range").refers_to_range
range_value = wb.names("named_range").refers_to_range.value
As mentioned by Felix, the http://docs.xlwings.org/en/stable/api.html#name page has the answer. Unfortunately, there are no examples of how to access the named range without first specifying a worksheet. I hope my explanation helps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Felix Zumstein |
| Solution 2 | |
| Solution 3 | Mactuary |
