Sorting Macro

aportman

New Member
Joined
Dec 23, 2004
Messages
37
I need help witht his. I am a beginner with macro's so bare with me. I am entering information into fields. I need the macro to sort them by grouping any alike fields together. Each field is number numerically 1 to 10. So I would like a macro or formula to enter the info as

1,3,4
2,5,6,7,8
9,10
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi
I gues you are talking about entering 1,3,4 in one cell in random order then sort in ascending order within the cell.
1) right click on sheet tab then click [ViewCode]
2) paste the code onto the right pane and click "x" to close the window
enter numbers in col.A separated by a comma.
Code:
Priavate Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
It Intersect(Target, Range("a:a")) Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In Intersect(Target, Range("a:a"))
     If InStr(r.Value, ",") > 0 Then
          x = Split(r.Value, ",")
          SortA x, 0, UBound(x)
          r.Value = Join(x,",")
     End If
Next
Application.EnableEvents = True
End Sub

Private Sub SortA(ary, LB, UB)
Dim i As Long, ii As Long, M As VAriant, temp As Variant
i = UB : ii = LB
M = Val(ary(Int((LB + UB)/2)))
Do While ii <= i
     Do While Val(ary(ii)) < M
         ii = ii + 1
     Loop
     Do While Val(ary(i)) > M
          i = i - 1
     Loop
     If ii <= i Then
          temp = ary(ii) : ary(ii) = ary(i) : ary(i) = temp
          i = i - 1 : ii = ii + 1
     End If
Loop
If LB < i Then SortA ary, LB, i
If ii < UB Then SortA ary, ii, UB
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,467
Members
448,965
Latest member
grijken

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