VBA: Check if a column is in alphabetical order

L

Legacy 143009

Guest
Hi Guys!
I need a script which checks the alphabetical order of a ray.
For Ex.

Code:
If (A2:A4) in alphabetical order Then
.
.
If not
.
.
End If

Thanks A lot!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Words but random words. I don't have a set for them. They are names and can be something like 168 different human names.
 
Upvote 0
How about:

Code:
Sub AlphaCompare()
If [A1] < [A2] And [A2] < [A3] And [A3] < [A4] Then
    MsgBox "items in alphabetic order"
Else
    MsgBox "items not in alphabetic order"
End If
End Sub
 
Upvote 0
Hah haa!! How I didn't see this! Of course it will work! thanks!!! :)

Edit: But if I want to check from A2, I can start the comparison from A2, ain't it?
 
Last edited by a moderator:
Upvote 0
Yes.......you can add or remove terms as required.


B.T.W.

If you have many items in a column, we would code it using a loop rather than a chain of Ands
 
Upvote 0
Hah haa!! How I didn't see this! Of course it will work! thanks!!! :)

Edit: But if I want to check from A2, I can start the comparison from A2, ain't it?

If you have more than 3 cells to compare in Column A, here is general code to do so...

Code:
LastRow = Cells(Rows.Count,"A").end(xlup).row 
If Evaluate("SUMPRODUCT(--(A2:A" & LastRow - 1 & ">A3:A" & LastRow & "))") then
  MsgBox "NOT in alphabetical order"
Else
  MsgBox "In alphabetical order"
End If
 
Upvote 0
Very nice Rick..............Given that words as well as numbers can be compared with ">" and "<", I wonder how difficult it would be to create a VBA MAX() and MIN() function for words??
 
Upvote 0
This function will return 1 if the range argument is sorted ascending, -1 if descending, 0 otherwise.
Note: 1, 2, 2, 3, 4, is not considered to be "sorted".
Code:
Function OrderedBy(someRange as Range) as Long
    Dim i As Long

    For i = 1 to someRange.Cells.Count
        If Application.CountIf(someRange, "<" & CStr(someRange.Item(i).Value) = (i - 1) Then
            OrderedBy = OrderedBy + 1
        End If
        If Application.CountIf(someRange, ">" & CStr(someRange.Item(i).Value = (i - 1) Then
            OrderedBy = OrderedBy - 1
        End If
    Next i

    If Abs(OrderedBy) = SomeRange.Cells.Count Then
        OrderedBy = Sgn(OrderedBy)
    Else
        OrderedBy = 0
    End If
End Function
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,938
Members
449,197
Latest member
k_bs

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