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
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))
Quite an easy and straight forward algorithm, once you know it.