# Sorting Macro

#### aportman

##### New Member
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

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
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``````

Replies
11
Views
625
Replies
10
Views
415
Replies
1
Views
535
Replies
1
Views
317
Replies
0
Views
204

1,221,009
Messages
6,157,360
Members
451,416
Latest member
Ilu

### 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.

### Which adblocker are you using?

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

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