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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
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
 

Forum statistics

Threads
1,181,373
Messages
5,929,571
Members
436,679
Latest member
helly123

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
Top