help with something quick and easy

Xcel90pro

New member
Joined
Jun 9, 2014
Messages
6
Reaction score
0
Points
0
So I found a sorting property to arrange numbers numerically but I have messed up something simple in the code

here is the website I got it from any way i could get a close fix


Sort Numbers in Excel VBA - Easy Excel Macros



Here is the code I got from is im missing something simple any ideas thanks guys

Code:
Sub start()
Code:
[COLOR=#333333]Dim i As Integer, j As Integer, temp As Integer, rng As Range[/COLOR]
[COLOR=#333333]
    Set rng = Range("A1").CurrentRegion[/COLOR]
[COLOR=#333333]    For i = 1 To rng.Count[/COLOR]
[COLOR=#333333]        For j = i + 1 To rng.Count[/COLOR]
[COLOR=#333333]            If rng.Cells(j) < rng.Cells(i) Then[/COLOR]

[COLOR=#333333]            End If[/COLOR]

[COLOR=#333333]            'swap numbers[/COLOR]
[COLOR=#333333]            temp = rng.Cells(i)[/COLOR]
[COLOR=#333333]            rng.Cells(i) = rng.Cells(j)[/COLOR]
[COLOR=#333333]            rng.Cells(j) = temp[/COLOR]

[COLOR=#333333]        Next j[/COLOR]

[COLOR=#333333]    Next i[/COLOR]

[COLOR=#333333]End sub[/COLOR]
 
Last edited by a moderator:

Bob Phillips

Super Moderator
Staff member
Joined
Mar 21, 2011
Messages
1,942
Reaction score
0
Points
36
Excel Version(s)
O365
That article was badly written, it didn't explain that you needed to insert the swap code betweenm the If ... EndIf

Code:
Sub start()
Dim i As Integer, j As Integer, temp As Integer, rng As Range


    Set rng = Range("A1").CurrentRegion
    For i = 1 To rng.Count
        For j = i + 1 To rng.Count
            If rng.Cells(j) < rng.Cells(i) Then


                'swap numbers
                temp = rng.Cells(i)
                rng.Cells(i) = rng.Cells(j)
                rng.Cells(j) = temp
            End If
        Next j
    Next i
End Sub
 
Top