Home

maintaining a cell reference despite changing the cell location through sorting

%3CLINGO-SUB%20id%3D%22lingo-sub-773525%22%20slang%3D%22en-US%22%3Emaintaining%20a%20cell%20reference%20despite%20changing%20the%20cell%20location%20through%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-773525%22%20slang%3D%22en-US%22%3E%3CP%3Eif%20the%20position%20of%20a%20value%20is%20changed%20through%20a%20sorting%20procedure%20the%20reference%20to%20the%20value%20in%20another%20cell%20is%20lost...%20e.g.%3A%20I%20reference%20cell%20A1%20filled%20with%20the%20value%201%20to%20which%20I%20reference%20in%20another%20cell%3B%20I%20now%20sort%20rows%20in%20a%20way%20so%20that%20the%20value%201%20moves%20to%20cell%20A3%20und%20the%20value%203%20is%20filled%20into%20cell%20A1%2C%20then%20the%20reference%20in%20the%20other%20cell%20is%20changed%20to%203.%20how%20can%20I%20maintain%20the%20reference%20to%20the%20value%201%2C%20i.e.%20to%20the%20cell%20that%20was%20'moved'%3F%3C%2FP%3E%3CP%3Ethanks!%3C%2FP%3E%3CP%3EBerlinerTourGuide%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-773525%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-774065%22%20slang%3D%22en-US%22%3ERe%3A%20maintaining%20a%20cell%20reference%20despite%20changing%20the%20cell%20location%20through%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-774065%22%20slang%3D%22en-US%22%3EFilters%20do%20not%20follow%20the%20rules%20of%20maintaining%20cell%20references.%20The%20function%20%3DA1%20references%20the%20cell%20A1%20not%20the%20value%20in%20A1.%20What%20I%20mean%20by%20this%20is%20that%20when%20you%20filter%20your%20table%2C%20%3DA1%20will%20always%20refer%20to%20whatever%20value%20is%20held%20in%20cell%20A1%2C%20not%20the%20value%20that%20was%20originally%20in%20cell%20A1%20before%20the%20filter%20was%20initiated.%20If%20your%20table%20permits%20it%2C%20VLOOKUP()%20may%20be%20a%20viable%20solution%20to%20your%20problem.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-774126%22%20slang%3D%22en-US%22%3ERe%3A%20maintaining%20a%20cell%20reference%20despite%20changing%20the%20cell%20location%20through%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-774126%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F382102%22%20target%3D%22_blank%22%3E%40BerlinerTourGuide%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%0A%3CP%3EThe%20workaround%20with%20the%20reference%20could%20be%20organize%20as%3C%2FP%3E%0A%3CP%3E(assuming%20your%20data%20is%20in%20column%20A%20starting%20from%20cell%20A1%20and%20you%20reference%20on%20the%20value%20of%20the%20cell%20in%2015th%20position%2C%20other%20words%20on%20A15%20in%20unfiltered%20range)%3C%2FP%3E%0A%3CPRE%3E%3DINDEX(%24A%3A%24A%2C%0A%20%20COUNTA(%24A%241%3AINDEX(%24A%3A%24A%2CROW(A15)-ROW(%24A%241)%2B1))%20-%0A%20%20SUBTOTAL(103%2C%24A%241%3AINDEX(%24A%3A%24A%2CROW(A15)-ROW(%24A%241)%2B1))%20%2B%0A%20%20ROW(A15)-ROW(%24A%241)%2B1%0A)%3C%2FPRE%3E%0A%3CP%3ENot%20very%20practical...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-774313%22%20slang%3D%22en-US%22%3ERe%3A%20maintaining%20a%20cell%20reference%20despite%20changing%20the%20cell%20location%20through%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-774313%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3B%3A%20thanks%2C%20but%20indeed%3A%20not%20very%20practical...%3C%2FP%3E%3CP%3EI%20have%20lots%20of%20rows%20which%20belong%20to%20different%20categories%20(marked%20out%20in%20various%20columns).%20now%20I'd%20like%20-%20in%20a%20simple%20way)%20to%20sort%20(not%20filter%2C%20like%20PReagan%20said)%20the%20rows%20according%20to%20the%20categories%2C%20then%20form%20sums%20of%20the%20grouped%20row-categories%2C%20reference%20them%2C%20and%20then%20change%20the%20order%20of%20the%20rows%20again...%20so%20what%20would%20be%20needed%20is%20an%20automatic%20transformation%20of%20e.g.%20a1%3Aa3%20into%20a1%2Ba2%2Ba3%20and%20then%20links%20that%20move%20automatically%20with%20each%20of%20the%20re-sorted%20cells...%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3Bnote%3A%20this%20is%20the%20first%20time%20that%20I%20could%20not%20solve%20a%20task%20with%20excel...%20%3CLI-EMOJI%20id%3D%22lia_winking-face%22%20title%3D%22%3Awinking_face%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-799302%22%20slang%3D%22en-US%22%3ERe%3A%20maintaining%20a%20cell%20reference%20despite%20changing%20the%20cell%20location%20through%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-799302%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3Band%20'the%20world'%3A%20well%2C%20I%20have%20a%20work-around%2C%20at%20least%20for%20the%20special%2C%20but%20very%20frequent%20case%20of%20sums%3A%20if%20you%20had%20marked%20a%20certain%20minor%20block%20of%20numbers%20as%20a%20sum%2C%20and%20then%20change%20the%20order%20completely%20via%20sorting%2C%20the%20sum%20would%20be%20wrong%20afterwards...%20to%20avoid%20this%20one%20must%20open%20an%20auxiliary%20column%20(which%20can%20be%20removed%20afterwords%20from%20view)%3B%20in%20this%20column%20one%20references%20the%20first%20number%20of%20the%20block%2C%20then%20drags%20down%20to%20the%20end%20of%20the%20block%3B%20so%20you%20reference%20all%20intended%20numbers%20of%20the%20block%3B%20then%20in%20the%20(visible)%20column%20that%20'counts'%20you%20write%20a%20sum%20that%20includes%20all%20lines%20of%20your%20sheet%20(as%20many%20lines%20as%20there%20are%2C%20from%20the%20first%20to%20the%20last...%20so%20you%20may%20have%3A%20sum(a1%3Aa1000)%20but%20only%20three%20numbers%20in%20that%20column...)%3B%20now%20you%20can%20change%20the%20order%20of%20the%20lines%20as%20you%20whish%2C%20and%20the%20sum%20remains%20correct...%20well%2C%20almost...%20excel%20may%20change%20the%20range%20of%20the%20sum...%20(annoying...)%3B%20so%20either%20adjust%20the%20range%20which%20is%20quickly%20done%2C%20or%20place%20the%20sum%20in%20the%20first%20line%20or%20another%20line%20whose%20position%20is%20not%20changed...%3B)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-799593%22%20slang%3D%22en-US%22%3ERe%3A%20maintaining%20a%20cell%20reference%20despite%20changing%20the%20cell%20location%20through%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-799593%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F382102%22%20target%3D%22_blank%22%3E%40BerlinerTourGuide%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can.t%20help%20thinking%20that%20the%20problem%20stems%20from%20the%20'quick%20and%20dirty'%20philosophy%20that%20pervades%20spreadsheet%20practice%20(though%20it%20is%20responsible%20for%20its%20huge%20success%20in%20terms%20of%20adoption).%26nbsp%3B%20The%20idea%20of%20clicking%20on%20a%20cell%20to%20reference%20it%20by%20location%20(A1%20%3CEM%3Eetc%3C%2FEM%3E.)%20on%20the%20sheet%20means%20that%20the%20user%20action%20is%20incorporated%20into%20the%20model%20but%20their%20intent%20is%20not%20evident.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20present%20case%2C%20the%20data%20might%20be%20a%20list%20or%2C%20less%20likely%2C%20an%20array.%26nbsp%3B%20If%20it%20were%20an%20array%2C%20then%20order%20within%20the%20array%20is%20significant%20(data%20is%20access%20by%20an%20index%20or%20a%20combination%20of%20indices)%20and%20on%20no%20account%20should%20the%20order%20be%20changed%20by%20sorting.%26nbsp%3B%20Preventing%20sorting%20or%20at%20least%20identifying%20it%20as%20an%20error%20is%20more%20important%20than%20coping%20with%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAssuming%20your%20data%20is%20actually%20a%20list%2C%20then%20sorting%20is%20an%20inherent%20property%20of%20a%20list%20and%20direct%20references%20to%20other%20records%20by%20location%20should%20not%20be%20used%20because%20the%20property%20that%20is%20not%20conserved%20under%20a%20sort%20operation.%26nbsp%3B%20Reference%20is%20normally%20performed%20through%20the%20use%20of%20primary%2Fforeign%20key%20matches%20which%20pick%20up%20the%20required%20data%20irrespective%20of%20its%20location.%26nbsp%3B%20If%2C%20as%20you%20suggest%2C%20there%20is%20a%20requirement%20to%20aggregate%20a%20block%20of%20values%2C%20then%20members%20of%20the%20block%20need%20to%20be%20identifiable%20from%20their%20attributes.%26nbsp%3B%20SUMIFS%20will%20pick%20up%20the%20correct%20values%20irrespective%20of%20their%20sort%20order.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20summary%2C%20you%20get%20what%20you%20pay%20for.%26nbsp%3B%20Cheap%20and%20nasty%20shortcuts%20get%20the%20initial%20job%20done%20fast%20but%20can%20easily%20go%20wrong%20%E2%80%A6%20which%20might%20go%20a%20long%20way%20towards%20explaining%20the%20fact%20that%203rd-party%20audits%20have%20shown%20that%20fewer%20than%201%20in%2010%20spreadsheets%20are%20actually%20correct.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENote%3A%26nbsp%3B%20As%20an%20aside%2C%20if%20you%20are%20processing%20lists%2C%20ensure%20that%20they%20are%20held%20as%20Tables.%26nbsp%3B%20If%20it%20is%20simply%20a%20range%2C%20then%20it%20is%20only%20too%20easy%20to%20sort%20some%20of%20the%20fields%20whilst%20leaving%20others%20in%20their%20original%20order.%26nbsp%3B%20This%20destroys%20the%20integrity%20of%20the%20data%20and%20is%20irrecoverable%20once%20options%20for%20'Undo'%20have%20been%20exhausted.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-774311%22%20slang%3D%22en-US%22%3ERe%3A%20maintaining%20a%20cell%20reference%20despite%20changing%20the%20cell%20location%20through%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-774311%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F360420%22%20target%3D%22_blank%22%3E%40PReagan%3C%2FA%3E%3A%20thanks%2C%20but%20you%20repeat%20just%20the%20problem%20(which%20I%20have%20already%20understood).%20I'm%20looking%20for%20the%20solution...%20%3CLI-EMOJI%20id%3D%22lia_winking-face%22%20title%3D%22%3Awinking_face%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FP%3E%3CP%3Eps%3A%26nbsp%3B%3CSPAN%3Eand%20I%20was%20not%20talking%20about%20filtering%20(which%20is%20even%20more%20tricky)%3B%20I%20was%20talking%20about%20sorting...%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
BerlinerTourGuide
New Contributor

if the position of a value is changed through a sorting procedure the reference to the value in another cell is lost... e.g.: I reference cell A1 filled with the value 1 to which I reference in another cell; I now sort rows in a way so that the value 1 moves to cell A3 und the value 3 is filled into cell A1, then the reference in the other cell is changed to 3. how can I maintain the reference to the value 1, i.e. to the cell that was 'moved'?

thanks!

BerlinerTourGuide

6 Replies
Filters do not follow the rules of maintaining cell references. The function =A1 references the cell A1 not the value in A1. What I mean by this is that when you filter your table, =A1 will always refer to whatever value is held in cell A1, not the value that was originally in cell A1 before the filter was initiated. If your table permits it, VLOOKUP() may be a viable solution to your problem.

@BerlinerTourGuide ,

The workaround with the reference could be organize as

(assuming your data is in column A starting from cell A1 and you reference on the value of the cell in 15th position, other words on A15 in unfiltered range)

=INDEX($A:$A,
  COUNTA($A$1:INDEX($A:$A,ROW(A15)-ROW($A$1)+1)) -
  SUBTOTAL(103,$A$1:INDEX($A:$A,ROW(A15)-ROW($A$1)+1)) +
  ROW(A15)-ROW($A$1)+1
)

Not very practical...

@PReagan: thanks, but you repeat just the problem (which I have already understood). I'm looking for the solution... ;-)

ps: and I was not talking about filtering (which is even more tricky); I was talking about sorting...

@Sergei Baklan : thanks, but indeed: not very practical...

I have lots of rows which belong to different categories (marked out in various columns). now I'd like - in a simple way) to sort (not filter, like PReagan said) the rows according to the categories, then form sums of the grouped row-categories, reference them, and then change the order of the rows again... so what would be needed is an automatic transformation of e.g. a1:a3 into a1+a2+a3 and then links that move automatically with each of the re-sorted cells...

   note: this is the first time that I could not solve a task with excel... ;-)

@Sergei Baklan and 'the world': well, I have a work-around, at least for the special, but very frequent case of sums: if you had marked a certain minor block of numbers as a sum, and then change the order completely via sorting, the sum would be wrong afterwards... to avoid this one must open an auxiliary column (which can be removed afterwords from view); in this column one references the first number of the block, then drags down to the end of the block; so you reference all intended numbers of the block; then in the (visible) column that 'counts' you write a sum that includes all lines of your sheet (as many lines as there are, from the first to the last... so you may have: sum(a1:a1000) but only three numbers in that column...); now you can change the order of the lines as you whish, and the sum remains correct... well, almost... excel may change the range of the sum... (annoying...); so either adjust the range which is quickly done, or place the sum in the first line or another line whose position is not changed...

@BerlinerTourGuide 

I can.t help thinking that the problem stems from the 'quick and dirty' philosophy that pervades spreadsheet practice (though it is responsible for its huge success in terms of adoption).  The idea of clicking on a cell to reference it by location (A1 etc.) on the sheet means that the user action is incorporated into the model but their intent is not evident. 

 

In the present case, the data might be a list or, less likely, an array.  If it were an array, then order within the array is significant (data is access by an index or a combination of indices) and on no account should the order be changed by sorting.  Preventing sorting or at least identifying it as an error is more important than coping with it.

 

Assuming your data is actually a list, then sorting is an inherent property of a list and direct references to other records by location should not be used because the property that is not conserved under a sort operation.  Reference is normally performed through the use of primary/foreign key matches which pick up the required data irrespective of its location.  If, as you suggest, there is a requirement to aggregate a block of values, then members of the block need to be identifiable from their attributes.  SUMIFS will pick up the correct values irrespective of their sort order.

 

In summary, you get what you pay for.  Cheap and nasty shortcuts get the initial job done fast but can easily go wrong … which might go a long way towards explaining the fact that 3rd-party audits have shown that fewer than 1 in 10 spreadsheets are actually correct.

 

Note:  As an aside, if you are processing lists, ensure that they are held as Tables.  If it is simply a range, then it is only too easy to sort some of the fields whilst leaving others in their original order.  This destroys the integrity of the data and is irrecoverable once options for 'Undo' have been exhausted.

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
35 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
9 Replies