Home > database >  Reverser function that can group the value whit in a cell
Reverser function that can group the value whit in a cell

Time:04-08

I have been searching and doing some study on all the different posts I could find but can't say this request or question has been discussed before.

This is basic reverse funktion, what I´ll like to accomplish is (as I call it) a group reversed function. Will Attach a picture to graphicly explain this better.

The goal is then to use =StrReverse($A1)

Function Reversestr(str As String) As String
    Reversestr = StrReverse(Trim(str))
End Function

enter image description here

CodePudding user response:

Please, use the next function:

Function reversePairOfDigits(strText As String) As String
    Dim i As Long, strRes As String
    For i = 1 To Len(strText) Step 2
        strRes = Mid(strText, i, 2) & strRes
    Next
    reversePairOfDigits = strRes
End Function

It can be used in the next simple way. Select a cell containing the string to be processed and run it:

Sub testReversePairOfDigits()
    Debug.Print reversePairOfDigits(ActiveCell.value)
End Sub

You can see the result in Immediate Window (Ctrl G, being in VBE)

If the strings to be processed are in a specific range, is needed to iterate between its cells and call the supplied function. To make the code faster, the range should be placed in an array, then work on that and finally drop the processed result. If you clearly define the range to be processed, I can show you how to do it efficiently.

CodePudding user response:

Reverse String (UDF)

enter image description here

Option Explicit

Function ReverseString( _
    ByVal Word As String, _
    Optional ByVal CharCount As Long = 1) _
As String
    
    Dim wLen As Long: wLen = Len(Word)
    Dim First As Long: First = wLen Mod CharCount
    If First > 0 Then ReverseString = Left(Word, First)
    
    Dim n As Long
    
    For n = First   1 To wLen Step CharCount
        ReverseString = Mid(Word, n, CharCount) & ReverseString
    Next n
    
End Function
  • Related