Reversing Excel Cell Contents

Posted Wed, Sep 9 2009 17:43 by Nate Oliver

This is a bit of a tricky subject, in that it depends what you mean by reversing. This particular entry will explore reversing blocks of a String, versus an exact reverse of everything.

So, here's an example, how do I get from this:

4189 > 3743 > 2040 > 949 > 195 > 5

To this:

5 > 195 > 949 > 2040 > 3743 > 4189

There's no way to do this natively, in Excel, that I can think of. At least, not without really working your tail off. So, we'll, naturally, roll our own.

We'll make this a two-step process, a UDF to accept the data, and a Sub Routine to sort it. That would appear as the following:


Public Function stringReverse(ByRef strIn As String) As String
Dim tmpArr() As String
tmpArr = Split(strIn)
Call Reverse(tmpArr)
stringReverse = Join(tmpArr, " ")
End Function

Public Sub Reverse(ByRef lParams() As String)
Dim i As Long, tmp As String
For i = 0 To UBound(lParams) / 2 - 1
    tmp = lParams(i)
    lParams(i) = lParams(UBound(lParams) - i)
    lParams(UBound(lParams) - i) = tmp
Next i
End Sub


A couple of things to note.

1) This requires a VBA environment greater than Excel 97, as it uses String Functions that were introduced in VB6.
2) This is a fairly efficient process, you might note that the processing Sub Routine reduces the iteration against the array by 2.

There's a few different ways to implement this. The first is with VBA, e.g.,


Sub foo()
MsgBox stringReverse("4189 > 3743 > 2040 > 949 > 195 > 5")
End Sub


And you might be wondering at this point, why not use VBA's native strReverse() function? E.g.,


Sub bar()
MsgBox strReverse("4189 > 3743 > 2040 > 949 > 195 > 5")
End Sub


Well, strReverse() reverses everything a little too well. Notice what happens with 4189, for example - it becomes 9184 - not quite what we're after.

The other manner in which to use this, which is why we created a UDF as the initial call, is as a Worksheet User Defined Function call, e..g,

=stringreverse(A1)

In addition to the text examples in this blog post, I'm attaching a working example. It includes VBA and is attached with no assurances or warranties - open it at your own risk. Otherwise, have a nice evening.

Comments

# re: Reversing Excel Cell Contents

Wednesday, September 09, 2009 6:17 PM by Robert

Bob Umlas can probably come up with a non VBA soluition :-)

# re: Reversing Excel Cell Contents

Wednesday, September 09, 2009 7:24 PM by Nate Oliver

I hope so, I'd like to see it.

But I also would prefer for it to be very efficient, I'm not a believer that native is better when it requires a lot more overhead.

# re: Reversing Excel Cell Contents

Thursday, September 10, 2009 10:14 AM by Alan Wolfson

The result is not the same mathematically as the original. 5 is not greater than 195, etc.

# re: Reversing Excel Cell Contents

Thursday, September 10, 2009 10:51 AM by Nate Oliver

Understood, it's a contrived example, although based on a real question.

While contrived, there are scenarios where the smaller the number, the better. E.g., scratches on break, interceptions thrown in a season, payback period on investment, etc...

Leave a Comment

(required) 
(required) 
(optional)
(required)