The formula works great, can you explain the logic of the formula for my understanding.
This formula:
{=IF(SUM(--(A1:A9>A2:A10))=0,"Sorted","Not Sorted")}
can be broken down as follows:
A1:A9>A2:A10
Assuming the data is continuous in cells A1:A10 this part of the formula is comparing A1 against A2, A2 against A3, A3 against A4 and so on up to A9 against A10. The test A1>A2 returns TRUE if A1 exceeds A2, otherwise FALSE. If the data was sorted (in ascending order) then the array would look like this:
{FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE}
If the data was not sorted (let's assume A3<a2 and="" a9=""><a8) then="" the="" array="" would="" look="" like="" this:=""> < A2 and A9 < A8) then the array would look like this:
{FALSE;TRUE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;TRUE;FALSE}
The next part is:
--
The minus minus converts TRUE to 1 and FALSE to 0. Assuming the data was sorted the array would then be converted as follows:
{0;0;0;0;0;0;0;0;0;0}
otherwise it may look like this (if not sorted per my example):
{0;1;0;0;0;0;0;0;1;0}
The Sum function returns the sum of the array, in the first instance it returns
Sum({0;0;0;0;0;0;0;0;0;0}) = 0
or (if not sorted)
Sum({0;1;0;0;0;0;0;0;1;0}) = 2
The IF statement then says if the sum = 0 then the list is sorted, otherwise it isn't. You can check out the workings of a formula by selecting the cell and then selecting menu option Tools > Formula Auditing > Evaluate Formula. I wouldn't recommend doing this with large array formulas because I have seen Excel crash on occasion if the array is too large.
Technically the formula doesn't need 2 minus symbols - you could get away with using just one. I'm just in the habit of using 2. There would be other ways of doing this with a formula but this method sprung to mind when I read the question this morning. However, I believe this formula isn't necessarily right for the problem at hand given the OP is already using VBA which wasn't stated in the first post......
Andrew</a8)></a2>