slyclam
slyclam

Reputation: 290

Reverse excel cell contents using VB.net

I have a column (C) in Excel with around 400 rows (variable) with alphanumeric values. What I want is to reverse each cells content to be reversed i.e something like

Xy1234 ----> 4321yX

So far I have,

Sub Rev()
    Dim C As Excel.Range

    For Each C In sheet.UsedRange.Columns(1).Cells
        C(1, 8).FormulaR1C1 = "=RevStr(RC[-5])*1"
    Next C



End Sub

Public Function RevStr(ByVal Rng As Excel.Range)
    RevStr = StrReverse(Rng.Value)
End Function

But instead of the desired output all I get in column H is #NAME? in all the rows.

Upvotes: 0

Views: 220

Answers (2)

Jose C
Jose C

Reputation: 317

I think calling RevStr from .net and then write the result to the cell instead on calling it from Excel would work

Upvotes: 0

majjam
majjam

Reputation: 1326

You could add a module, add this function to it:

Option Explicit

Public Function ReverseContent(ByVal myRange As Range) As String
    ReverseContent = StrReverse(myRange.Value)
End Function

And then call it in your sheet using:

=ReverseContent(A1)

This wouldn't replace the contents of your cells of course, but you could easily copy and paste them over? In this way it performs like any other excel function.

Upvotes: 1

Related Questions