Hi;
I've got an array of values, that I need to put in reverse alphabetical order. The problem is that the values are spreadsheet columns, and things have finally pushed into the "AA" realm. Before, I was using this wonderful bit of code from TonyJollans:
However, now that I've gotten into AA, I can't use Chr and the Large function anymore. (at least, I think that's the problem).
So, now, schmuck that I am, I've gotten myself stuck, where I have to properly reverse-sort the array, and I've got till Friday to do it.
So, the details are: I have a comma-separated value string that contains all the columns I want to do an action to, so the string may be, say "c,b,x,z,y,aa,ac". I need to sort that into an array in reverse order (so it starts with array(1) = AC, array(2) = AA, array(3) = z, and so on).
So what I thought I'd do is make an array of single-character columns (xArray), and make another array of double-char columns(xxArray), that second one only containing the last letter, sort them both, and then joing them up, putting an "A" in front of every value in xxArray.
However, the following code craps out, and tells me it can't use Large. I've been unable to figure out why.
Does anyone know, or do you have a better way than this horrid cludge?
Note: in thise code, xCounter keeps track of the single-char array, xxCounter the double, and Counter the total number. Also note that in the final version, I won't be joining the values into a string, I'll be putting them into a single array and then doing my action by looping through that array. But the string makes it easy to debug The premise of the sorting is the same though.
Thanks! (and please don't laugh)
I've got an array of values, that I need to put in reverse alphabetical order. The problem is that the values are spreadsheet columns, and things have finally pushed into the "AA" realm. Before, I was using this wonderful bit of code from TonyJollans:
Code:
Dim arrTemp(), SortedString
ReDim arrTemp(1 To Len(YourString))
For i = 1 To Len(YourString)
arrTemp(i) = Asc(Mid(YourString, i, 10))
Next
For i = 1 To Len(YourString)
SortedString = SortedString & Chr(WorksheetFunction.Large(arrTemp, i))
However, now that I've gotten into AA, I can't use Chr and the Large function anymore. (at least, I think that's the problem).
So, now, schmuck that I am, I've gotten myself stuck, where I have to properly reverse-sort the array, and I've got till Friday to do it.
So, the details are: I have a comma-separated value string that contains all the columns I want to do an action to, so the string may be, say "c,b,x,z,y,aa,ac". I need to sort that into an array in reverse order (so it starts with array(1) = AC, array(2) = AA, array(3) = z, and so on).
So what I thought I'd do is make an array of single-character columns (xArray), and make another array of double-char columns(xxArray), that second one only containing the last letter, sort them both, and then joing them up, putting an "A" in front of every value in xxArray.
However, the following code craps out, and tells me it can't use Large. I've been unable to figure out why.
Does anyone know, or do you have a better way than this horrid cludge?
Note: in thise code, xCounter keeps track of the single-char array, xxCounter the double, and Counter the total number. Also note that in the final version, I won't be joining the values into a string, I'll be putting them into a single array and then doing my action by looping through that array. But the string makes it easy to debug The premise of the sorting is the same though.
Code:
Sub ArraySort()
Dim Value, xArray(), xxArray(), SortedString, YourString, i, Counter, xCounter, xxCounter
Dim Done As Boolean
YourString = "a,b,c,ac,d,aa"
Counter = 0
xCounter = 0
xxCounter = 0
Done = False
Do Until Done = True
If InStr(YourString, ",") > 0 Then
Value = Left(YourString, InStr(YourString, ",") - 1)
YourString = Right(YourString, Len(YourString) - Len(Value) - 1)
Else
Value = YourString
Done = True
End If
If Len(Value) = 1 Then
xCounter = xCounter + 1
ReDim Preserve xArray(xCounter)
xArray(xCounter) = Value
ElseIf Len(Value) = 2 Then
xxCounter = xxCounter + 1
ReDim Preserve xxArray(xxCounter)
xxArray(xxCounter) = Right(Value, 1)
Else
MsgBox ("Error")
End If
Counter = Counter + 1
Loop
For i = 1 To xxCounter
SortedString = SortedString & "A" & Chr(WorksheetFunction.Large(xxArray, i))
Next i
For i = 1 To xCounter
SortedString = SortedString & Chr(WorksheetFunction.Large(xArray, i))
Next i
MsgBox (SortedString)
End Sub
Thanks! (and please don't laugh)