Reversing words in a sentence with VBA in Excel is quite easy, using the built-in StrReverse function.
StrReverse takes a string and reverses it. Thus, “VitoshAcademy.com” becomes “moc.ymedacAhsotiV”:
The idea of reversing the order of the words in a sentence needs a couple of more steps :
- reversing the sentence once
- splitting the reversed words to an array
- writing down the words and reversing them one by one
1 2 3 4 5 6 7 8 9 10 11 |
Function ReverseSentence(inputString As String) Dim word As Variant Dim reversed As String reversed = StrReverse(inputString) For Each word In Split(reversed) ReverseSentence = Trim(ReverseSentence & " " & StrReverse(word)) Next End Function |
Quite an easy and straight forward algorithm, once you know it.