diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCellsCollection.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCellsCollection.cs index 4ac6497..fae4549 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCellsCollection.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCellsCollection.cs @@ -297,7 +297,7 @@ if (cell1 != null) { cell1.Address = new XLAddress(cell1.Worksheet, sp2.Row, sp2.Column, false, false); - _cellsDictionary.Remove(sp1); + Remove(sp1); if (cell2 != null) Add(sp1, cell2); } @@ -305,7 +305,7 @@ if (cell2 == null) continue; cell2.Address = new XLAddress(cell2.Worksheet, sp1.Row, sp1.Column, false, false); - _cellsDictionary.Remove(sp2); + Remove(sp2); if (cell1 != null) Add(sp2, cell1); } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs index 868e46e..3a6f014 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs @@ -1702,6 +1702,9 @@ Int32 maxRow = RowCount(); if (maxRow == XLHelper.MaxRowNumber) maxRow = LastCellUsed(true).Address.RowNumber; + + // Call all cells to make sure they exist even if empty + Worksheet.Range(1, 1, maxRow, RangeAddress.LastAddress.ColumnNumber).Cells().ForEach(c=> { }); SortingRangeRows(1, maxRow); } diff --git a/ClosedXML/ClosedXML/ClosedXML_Tests/Resource/Examples/Ranges/SortExample.xlsx b/ClosedXML/ClosedXML/ClosedXML_Tests/Resource/Examples/Ranges/SortExample.xlsx index 822f798..cfa7b7b 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Tests/Resource/Examples/Ranges/SortExample.xlsx +++ b/ClosedXML/ClosedXML/ClosedXML_Tests/Resource/Examples/Ranges/SortExample.xlsx Binary files differ