check if a range of strings is sorted?

blitzo

New Member
Joined
Sep 28, 2011
Messages
2
I have a list of strings as such

A1: A
A2: B
A3: D
A4: E

How can I check to see if this range is sorted with vba?

I have searched but havent found anything for strings.

I can only think of adding each cell to an array, creating a copy array, sorting the copy array and then comparing the two arrays. I want it to return TRUE, FALSE or 1, 0

Thanks in advance!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Test()<br>    MsgBox "Range A1:A4 is sorted: " & IsSorted(Range("A1:A4"))<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> IsSorted(rng <SPAN style="color:#00007F">As</SPAN> Range, _<br>                          <SPAN style="color:#00007F">Optional</SPAN> SortOrder <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> = xlAscending, _<br>                          <SPAN style="color:#00007F">Optional</SPAN> Compare <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> = vbTextCompare) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br>    <br>    <SPAN style="color:#007F00">' SortOrder = xlDescending(Z to A) or xlAscending(A to Z default)</SPAN><br>    <br>    <SPAN style="color:#007F00">' Compare = vbBinaryCompare(case sensitive) or</SPAN><br>    <SPAN style="color:#007F00">'           vbTextCompare(default and not case sensitive)</SPAN><br>    <br>    <SPAN style="color:#00007F">Dim</SPAN> v <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, k <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN><br>    <br>    IsSorted = <SPAN style="color:#00007F">True</SPAN><br>    v = rng<br>    <SPAN style="color:#00007F">If</SPAN> SortOrder = xlDescending <SPAN style="color:#00007F">Then</SPAN> j = 1 <SPAN style="color:#00007F">Else</SPAN> k = 1<br>        <br>    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(v, 1) - 1<br>        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">StrComp</SPAN>(v(i + j, 1), v(i + k, 1), Compare) > 0 <SPAN style="color:#00007F">Then</SPAN><br>            IsSorted = <SPAN style="color:#00007F">False</SPAN><br>            <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">For</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN></FONT>
 
Last edited:
Upvote 0
Holy atomic pile, Batman!

Perfecto. Thanks so much. Much appreciate. Much better than the code i came up with.
 
Upvote 0
Your welcome.

Note: it only works on column data as is. With some small changes it could work on row data or both.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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