sort whole ranges based on selection multiple cells at once without any criteria

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
Hi experts ,
I no know if this way could be sort it !

I want to sort the data based on my choices, where the data will be arranged based on who will be selected first for cells in column A , then the next based on the selection of a cell or several cells at the same time. In this case, it will be arranged in an orderly manner and if I select new data are not selected before then will copy directly to below data have already selected and sorted . And in the case of choosing the same elements that were previously organized, then they are organized again and make the ones below them rise to the top

original data
Elmarghanie Brand .xlsm
ABCDE
1ITEMCODEBRANDPURCHASESELLING
21CCR-1220BS 1200 R20 18PR G580 THI20
32CCR-1221BS 1200 R20 18PR G580 JAP30471
43CCR-1222BS 1200R20-18PR R187 JAP9
54CCR-1223BS 1200R24 G580508
65CCR-1224BS 1200R24 G5822
76CCR-1225BS 13 R22.5 R187 JAP4
87CCR-1226BS 1400R20 TCF R180 JAP2
98CCR-1227BS 1400R20 TCF R180BZ JAP4
109CCR-1211BS 1400R20VSJ TCF JAP2255
1110CCR-1212BS 165 R13C R624 IND44
1211CCR-1213BS 175/70R13 EP150 THI2
1312CCR-1214BS 185/65R15 B250 JAP2
1413CCR-1215BS 185/65R15 T001 JAP1
1514CCR-1216BS 195/75R16C R613 JAP6
1615CCR-1217BS 195R14C 613 JAP4
1716CCR-1218BS 195R14C R623 THI4
1817CCR-1219BS 195R15C 613V JAP44
1918CCR-1228BS 205/60R16 T005 THI8
2019CCR-1229BS 205/70R15C R623 THI8405
2120CCR-1230BS 205R16C D840 THI26
2221CCR-1231BS 215/50 R17 EP300 THI10
2322CCR-1232BS 215/55R17 T005 JAP4
2423CCR-1233BS 215/60R16 ER30 JAP88
2524CCR-1234BS 215/65R15 T005A IND44
2625CCR-1235BS 215/70R15C R624 HD109S107S 8 TURK6
2726CCR-1236BS 225/40R18PR 005A JAP8
2827CCR-1237BS 225/55R17 EP300 THI1
2928CCR-1238BS 225/55R17 T005A JAP8
3029CCR-1239BS 225/55R18 AL001 JAP4
3130CCR-1240BS 225/60R17 D-SPORT JAP15
3231CCR-1241BS 225/60R18 ALENZA001 JAP4
3332CCR-1242BS 225/70R15C R623 JAP8
3433CCR-1243BS 235/35ZR19 RE050A JAP4
3534CCR-1244BS 235/50R18 AL01 JAP8
3635CCR-1245BS 235/55R17 ER300 JAP8
3736CCR-1246BS 235/65R16C R660 TURK12
3837CCR-1247BS 245/40R18PR 050A JAP8
3938CCR-1248BS 245/40R19 T005 JAP4
4039CCR-1249BS 245/40ZR20 PSPORT JAP4
4140CCR-1250BS 245/45R17 T05A JAP4
4241CCR-1251BS 245/45R18 EP300 THI4
4342CCR-1252BS 245/45R19 T005A JAP4
4443CCR-1253BS 245/70R17 684A JAP2
4544CCR-1254BS 255/35R19 S001 JAP4
4645CCR-1255BS 255/50R20 AL001 JAP8
4746CCR-1256BS 255/70R15C D840 THI33
4847CCR-1257BS 265/55R20 D693 LCRUSER JAP4
4948CCR-1158BS 265/60R18 D840 JAP4
5049CCR-1159BS 265/65R17 D840 JAP4
5150CCR-1160BS 265/65R18 D693 JAP4
5251CCR-1161BS 265/70R16 D840 THI76
5352CCR-1162BS 275/35R21 RE21 JAP4
5453CCR-1163BS 275/65R18 AL01 JAP12
5554CCR-1164BS 285/60R18 DHPS JAP8
5655CCR-1165BS 285/65R17 R683 JAP8
5756CCR-1166BS 295/35R20 S001 JAP4
5857CCR-1167BS 315/80R22.5-18PR G580 JAP12
5958CCR-1168BS 315/80R22.5-18PR G582 THI12
6059CCR-1169BS 325/95 R24 M840 JAP62
6160CCR-1170BS 385/65 R22.5 R164 JAP4
6261CCR-1171BS 445/65R22.5 R164 JAP24
6362CCR-1172BS 650R16 R230 JAP8
6463CCR-1173BS 385/65 R22.5 R164 JAP16
6564CCR-1174BS 445/65R22.5 R164 JAP18
6665CCR-1175BS 650R16 R230 JAP20
REPORT

examples:
pic: select cells A15,A19,A23
1.PNG
View attachment 87306
to become like this
Elmarghanie Brand .xlsm
ABCDE
1ITEMCODEBRANDPURCHASESELLING
21CCR-1216BS 195/75R16C R613 JAP6
32CCR-1228BS 205/60R16 T005 THI8
43CCR-1232BS 215/55R17 T005 JAP4
54CCR-1220BS 1200 R20 18PR G580 THI20
65CCR-1221BS 1200 R20 18PR G580 JAP30471
76CCR-1222BS 1200R20-18PR R187 JAP9
87CCR-1223BS 1200R24 G580508
98CCR-1224BS 1200R24 G5822
109CCR-1225BS 13 R22.5 R187 JAP4
1110CCR-1226BS 1400R20 TCF R180 JAP2
1211CCR-1227BS 1400R20 TCF R180BZ JAP4
1312CCR-1211BS 1400R20VSJ TCF JAP2255
1413CCR-1212BS 165 R13C R624 IND44
1514CCR-1213BS 175/70R13 EP150 THI2
1615CCR-1214BS 185/65R15 B250 JAP2
1716CCR-1215BS 185/65R15 T001 JAP1
1817CCR-1217BS 195R14C 613 JAP4
1918CCR-1218BS 195R14C R623 THI4
2019CCR-1219BS 195R15C 613V JAP44
2120CCR-1229BS 205/70R15C R623 THI8405
2221CCR-1230BS 205R16C D840 THI26
2322CCR-1231BS 215/50 R17 EP300 THI10
2423CCR-1233BS 215/60R16 ER30 JAP88
2524CCR-1234BS 215/65R15 T005A IND44
EXAMPLE1



pic2 to select A5,A13,A21
2.PNG
View attachment 87307
to become to data have already sorted bottom
Elmarghanie Brand .xlsm
ABCDE
1ITEMCODEBRANDPURCHASESELLING
21CCR-1216BS 195/75R16C R613 JAP6
32CCR-1228BS 205/60R16 T005 THI8
43CCR-1232BS 215/55R17 T005 JAP4
54CCR-1223BS 1200R24 G580508
65CCR-1214BS 185/65R15 B250 JAP2
76CCR-1230BS 205R16C D840 THI26
87CCR-1220BS 1200 R20 18PR G580 THI20
98CCR-1221BS 1200 R20 18PR G580 JAP30471
109CCR-1222BS 1200R20-18PR R187 JAP9
1110CCR-1224BS 1200R24 G5822
1211CCR-1225BS 13 R22.5 R187 JAP4
1312CCR-1226BS 1400R20 TCF R180 JAP2
1413CCR-1227BS 1400R20 TCF R180BZ JAP4
1514CCR-1211BS 1400R20VSJ TCF JAP2255
1615CCR-1212BS 165 R13C R624 IND44
1716CCR-1213BS 175/70R13 EP150 THI2
1817CCR-1215BS 185/65R15 T001 JAP1
1918CCR-1217BS 195R14C 613 JAP4
2019CCR-1218BS 195R14C R623 THI4
2120CCR-1219BS 195R15C 613V JAP44
2221CCR-1229BS 205/70R15C R623 THI8405
2322CCR-1231BS 215/50 R17 EP300 THI10
2423CCR-1233BS 215/60R16 ER30 JAP88
EXAMPLE2


pic3:
select cells A3,A25,A19,A13 to become like this
3.PNG
View attachment 87308
Elmarghanie Brand .xlsm
ABCDE
1ITEMCODEBRANDPURCHASESELLING
21CCR-1216BS 195/75R16C R613 JAP6
32CCR-1232BS 215/55R17 T005 JAP4
43CCR-1223BS 1200R24 G580508
54CCR-1221BS 1200 R20 18PR G580 JAP30471
65CCR-1234BS 215/65R15 T005A IND44
76CCR-1228BS 205/60R16 T005 THI8
87CCR-1214BS 185/65R15 B250 JAP2
98CCR-1230BS 205R16C D840 THI26
109CCR-1220BS 1200 R20 18PR G580 THI20
1110CCR-1222BS 1200R20-18PR R187 JAP9
1211CCR-1224BS 1200R24 G5822
1312CCR-1225BS 13 R22.5 R187 JAP4
1413CCR-1226BS 1400R20 TCF R180 JAP2
1514CCR-1227BS 1400R20 TCF R180BZ JAP4
1615CCR-1211BS 1400R20VSJ TCF JAP2255
1716CCR-1212BS 165 R13C R624 IND44
1817CCR-1213BS 175/70R13 EP150 THI2
1918CCR-1215BS 185/65R15 T001 JAP1
2019CCR-1217BS 195R14C 613 JAP4
2120CCR-1218BS 195R14C R623 THI4
2221CCR-1219BS 195R15C 613V JAP44
2322CCR-1229BS 205/70R15C R623 THI8405
2423CCR-1231BS 215/50 R17 EP300 THI10
2524CCR-1233BS 215/60R16 ER30 JAP88
EXAMPLE3



so how sort these data again:
1-should arrange based on selected multiple cells in column who first, second, third ....... then arrange the whole multiple ranges

2- if I select different data then will arrange directly to data have already sorted (under data as in point1) bottom

3- if I select data have already sorted before then will arrange again and delete from old location and rise data have already existed to up
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I think I've managed. Bind your button to mySort() function:
VBA Code:
Dim mySelection() As Long
Dim counter As Long
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Columns.Count = 1 And Not Intersect(Target, Columns(1)) Is Nothing And Intersect(Target, Range("A1")) Is Nothing Then
    ReDim Preserve mySelection(1 To Target.Count)
    mySelection(Target.Count) = Range(Split(Target.Address, ",")(Target.Count - 1)).Value
  End If
End Sub
Sub mySort()
  For Each sel In mySelection
    Rows(Application.Match(sel, Range("A:A"), 0)).Cut
    Rows(counter + 1).Offset(1).Insert
    counter = counter + 1
  Next
  For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
    Cells(i + 1, 1).Value = i
  Next
End Sub
 
Upvote 0
thanks
it gives error for loop not initialized in this line
VBA Code:
For Each sel In mySelection
 
Upvote 0
Yes, the code is not working when you select with dragging. I modified the code. I haven't encountered any problems but please let me know if you find a bug.
VBA Code:
Dim mySelection() As Long, counter As Long, i As Long
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Columns.Count = 1 And Not Intersect(Target, Columns(1)) Is Nothing And Intersect(Target, Range("A1")) Is Nothing Then
    Application.EnableEvents = False
    Dim tmp1 As String, tmp2 As String, k As Long, j As Long
    ReDim Preserve mySelection(1 To Target.Count)
    k = 1
    For i = 0 To UBound(Split(Target.Address, ","))
      tmp1 = Split(Target.Address, ",")(i)
      For j = 0 To UBound(Split(tmp1, ":"))
        tmp2 = Split(tmp1, ":")(j)
        mySelection(k) = CLng(Range(tmp2).Value)
        k = k + 1
      Next
    Next
    Application.EnableEvents = True
  End If
End Sub
Sub mySort()
  Application.ScreenUpdating = False
  For Each sel In mySelection
    Rows(Application.Match(sel, Range("A:A"), 0)).Cut
    Rows(counter + 1).Offset(1).Insert
    counter = counter + 1
  Next
  For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row - 1
    Cells(i + 1, 1).Value = i
  Next
  Cells(counter, 1).Select
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Selected multiple cells (2,4,5):
1678869749004.png

Run the code:
1678869786871.png

Sorted:
1678869829137.png


Sorry that I can help you no further.
 
Upvote 0
Application.EnableEvents may left False. First run this piece of code. Close and open the file again and try again:
VBA Code:
Sub test()
  Application.EnableEvents = True
End Sub
Using Application.EnableEvents is safe but you may remıbe these lines from the code if they are going to be problematic for you.
 
Upvote 0
Application.EnableEvents may left False. First run this piece of code. Close and open the file again and try again:
VBA Code:
Sub test()
  Application.EnableEvents = True
End Sub
Using Application.EnableEvents is safe but you may remıbe these lines from the code if they are going to be problematic for you.
Have you tried this?

Next time try to use the code like this:
VBA Code:
Dim mySelection() As Long, counter As Long, i As Long
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Columns.Count = 1 And Not Intersect(Target, Columns(1)) Is Nothing And Intersect(Target, Range("A1")) Is Nothing Then
    'Application.EnableEvents = False
    Dim tmp1 As String, tmp2 As String, k As Long, j As Long
    ReDim Preserve mySelection(1 To Target.Count)
    k = 1
    For i = 0 To UBound(Split(Target.Address, ","))
      tmp1 = Split(Target.Address, ",")(i)
      For j = 0 To UBound(Split(tmp1, ":"))
        tmp2 = Split(tmp1, ":")(j)
        mySelection(k) = CLng(Range(tmp2).Value)
        k = k + 1
      Next
    Next
    'Application.EnableEvents = True
  End If
End Sub
Sub mySort()
  Application.ScreenUpdating = False
  For Each sel In mySelection
    Rows(Application.Match(sel, Range("A:A"), 0)).Cut
    Rows(counter + 1).Offset(1).Insert
    counter = counter + 1
  Next
  For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row - 1
    Cells(i + 1, 1).Value = i
  Next
  Cells(counter, 1).Select
  Application.ScreenUpdating = True
End Sub
I don't know why you are having this error. The code works perfectly fine in my test file. It is impossible for me to troubleshoot your problem.
 
Upvote 0
ok your last version works perfectly as you said in your attaching , but unfortunately doesn't work my real file .
much appreciated if you're capable to see what's the problem for my real file .
Elmarghanie Brand .xlsm
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top