Share via

Word Reverse Text

Anonymous
2023-02-16T02:31:57+00:00

Hello From Steve

How to I reverse the below using VBA please

From

$14,000  1550

To

1550 $14,000

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2023-02-16T05:12:25+00:00

Hello Steve8d,

Thanks for contacting us. I am glad to help you with your request. You can use VBA to reverse the order of the words in a string. Here's an example of how to do this for your specific example:

Sub ReverseWords() Dim str As String Dim words() As String Dim i As Long

' Change this string to the text you want to reverse str = "From $14,000 1550"

' Split the string into an array of words words = Split(str, " ")

' Reverse the order of the words in the array For i = 0 To UBound(words) / 2 Dim temp As String temp = words(i) words(i) = words(UBound(words) - i) words(UBound(words) - i) = temp Next i

' Join the array of words back into a single string str = Join(words, " ")

' Print the reversed string to the Immediate window Debug.Print str End Sub

In this example, the string "From $14,000 1550" is first split into an array of words using the Split function. Then, a For loop is used to reverse the order of the words in the array. Finally, the Join function is used to join the array of words back into a single string. The reversed string is then printed to the Immediate window using the Debug.Print statement.

You can modify this code to reverse the order of the words in any string by changing the value of the str variable to the text you want to reverse.

Regards, Akande

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-02-16T05:46:11+00:00

    Hello Daniel

    Firstly thank you for your reply.

    Interesting "Print the reversed string to the Immediate window"

    I used $14000 1550, as an example Hoping to reverse on a document.

    Is it possible please to modify to allow on an actual document.

    All the best Steve

    Was this answer helpful?

    0 comments No comments