Forum Discussion
Why do worksheet XML files store shared formulas 2+ times instead of once?
Currently running Excel 365 (16.0.13801). I've been diving into the XML files within some .xlsx files and the way that XML stores shared formulas seems inefficient and slightly confounding. For my example, I'm using just a simple 10x10 multiplication table anchored in B2. In this example, I enter $B3*C$2 and copy that formula through the entire table. However, I noticed that when I copy a formula across a row and/or down a column, the XML initializes the shared formula on the second instance, rather than the first, like this:
<c r="C3">
<f>$B3*C$2</f>
<v>1</v>
</c>
<c r="D3">
<f t="shared" ref="D3:L12" si="0">$B3*D$2</f>
<v>2</v>
</c>
<c r="E3">
<f t="shared" si="0"/>
<v>3</v>
</c>...
The formula is entered in C3, and then is copied across and down, but the "shared" formula si="0" is initialized in D3, saying it runs from D3:L12, leaving that block, as far as I can tell, disconnected from column C entirely. That means, since it's being copied down AND across in this example, another shared formula s="1" is initialized in C4, and copies the formula down from C4:C12. So in this example the XML is storing the same formula 3 times. Once for the original cell C3, once for all cells in the block to the right, and once for all cells in column C directly beneath the original.
This all seemed unnecessary, so I went in and changed it to what made more sense to me, like this:
<c r="C3">
<f t="shared" ref="C3:L12" si="0">$B3*C$2</f>
<v>1</v>
</c>
<c r="D3">
<f t="shared" si="0"/>
<v>2</v>
</c>...
I just initialize it in the first cell and then everything down and right of it has <f t="shared" si="0"/>. And it works. Excel is able to read it fine, it opens, everything runs smoothly, and it doesn't even try to revert it. So my questions are:
- Why does it do this? Is it a safeguard for a situation I can't think of right now?
- Is it the way I'm entering and copying across my formulas that's causing this behavior? I tried both copying across and down with ctrl+shift+direction and then paste commands as well as just copying the cell, highlighting the entire box and pasting, both yielded the same result. Is there a way of doing it that's "XML efficient"?
- Slightly different, but I also noticed that in one of my files, no attempt has been made to store a specific Index(Match()) formula as a shared formula. Are there certain formulas XML just doesn't store as shared? or is there max length for the formula string? Are there rules for these cases?
I assume that the XML is stored with the goal of making it as easy as possible to be read by Excel, but I can't see how this would be efficient. I could see that Excel might be more efficient reading in XML by rows vs columns, or by square ranges vs rows/columns/single cells, or vice versa, but in this case, it's taking a simple square range, and breaking it into a single cell, a column, AND a square range, and thus having to store the formula an additional two times. There must be something I'm missing.
I know this may seem unimportant, but I'm working on a project that involves XML parsing/manipulation, so I want to have a better understanding of how this works, what inefficiencies there are, and if they can be fixed. Thanks for any help in advance!
9 Replies
- JKPieterseSilver ContributorWhat happens if you don't enter in one cell and copy, but first select the region, write the formula and then press control+enter?
- tygloskiCopper ContributorLooks like that does work, and results in the same XML that I created in the second box of code above. Very interesting. The unfortunate bit is that it's not the most intuitive way to go about entering formulas so it's not really something you could recommend as a best practice. It seems like it's just a lazy algorithm to me. You've entered something in C3, and you want to fill the box, but instead of just going back and editing C3 to unify the whole range, it creates two new ranges to fill only the empty space in the box and leaves C3 alone.
- JKPieterseSilver ContributorDoes it help to save as the file to the xml strict format? I expect not, just curious and too tied up to try it myself.