Forum Discussion
Large amount of data sorting
- May 26, 2020
Apologies. You're correct. Small adjustment needed: change
If MyCol <> 1 Thento
If MyCol > 2 ThenRegards
About the first point. Sorry, I made a mistake. Everything works.
Regarding the second point. I did not mean expanding the diapason of cells to the maximum size, but using a function such as selecting a row (shift + space from the keyboard) and sorting with a heading from column "A". Maybe I'm wrong, and in macros it's better not to do that.
Example attached below.
Not entirely sure what you mean there, sorry.
The modified version below detects the last non-empty cell in each row and applies the sort on a range with that cell as the upper bound. Note that, if any of the blank cells in your range in fact contain the null string (""), e.g. as a result of formulas within those cells, then it will not give correct results - in that case let me know and I will modify the code.
I have also amended it so that it runs on whichever is the active sheet at the time of running.
Sub SortAllRows()
Dim LRow As Long
Dim i As Long
Dim MyRow As Range
Dim MyCol As Long
Dim MyKey As Range
With ActiveSheet
LRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 3 To LRow
MyCol = .Cells(i, .Columns.Count).End(xlToLeft).Column
If MyCol <> 1 Then
Set MyRow = .Cells(i, 2).Resize(, MyCol - 1)
Set MyKey = Application.Index(MyRow, 1)
MyRow.Sort Key1:=MyKey, Order1:=xlAscending, Orientation:=xlLeftToRight
End If
Next i
End With
End Sub
Regards
- Odintsov_MaksimMay 26, 2020Copper ContributorOK, worked.
Will be testing.
Thank you so much! - Jos_WoolleyMay 26, 2020Iron Contributor
Apologies. You're correct. Small adjustment needed: change
If MyCol <> 1 Thento
If MyCol > 2 ThenRegards
- Odintsov_MaksimMay 26, 2020Copper Contributor
Jos_Woolley File attached
- Jos_WoolleyMay 26, 2020Iron Contributor
Column A should not be affected whatsoever by the macro I posted. Can you post a workbook with an example in which column A is affected?
Regards
- Odintsov_MaksimMay 26, 2020Copper ContributorWell thank you.
It works.
Only one problem.
Column "A" contains the numbering of records. And column “A” should not be involved in the sorting procedure. The data from column "A" should always remain in the same place.
Regards, thanks