View Full Version : Request a macro
Ronas
11-09-2016, 08:52 AM
Hi
I have a list like the following list in word 2013:
word1
word2
word3
.
.
.
They should be sorted according to the last letter (Ascending)
If two words have the same last letter, should be sorted according to the letter penultimate and so on.
Thanks
gmayor
11-10-2016, 12:29 AM
Select the list and run the following macro.
Sub SortBackwards()
Dim strText As String
Dim oPara As Paragraph
Dim oSel As Range
Dim oRng As Range
Set oSel = Selection.Range
For Each oPara In oSel.Paragraphs
Set oRng = oPara.Range
oRng.End = oRng.End - 1
strText = oRng.Text
oRng.Text = StrReverse(strText)
Next oPara
oSel.Sort
For Each oPara In oSel.Paragraphs
Set oRng = oPara.Range
oRng.End = oRng.End - 1
strText = oRng.Text
oRng.Text = StrReverse(strText)
Next oPara
lbl_Exit:
Set oSel = Nothing
Set oPara = Nothing
Set oRng = Nothing
Exit Sub
End Sub
gmaxey
11-10-2016, 06:06 PM
Graham,
You beat me to this one. I was pondering over it thinking I would use an array and then had to make short day trip. Here is another approach:
Sub SortBackwardsII()
Dim oRng As Range
Dim arrWords() As String
Dim lngIndex As Long
Dim oPara As Paragraph
ReDim arrWords(0)
Set oRng = Selection.Range.Duplicate
For Each oPara In oRng.Paragraphs
arrWords(UBound(arrWords)) = StrReverse(oPara.Range.Words(1))
ReDim Preserve arrWords(UBound(arrWords) + 1)
Next oPara
ReDim Preserve arrWords(UBound(arrWords) - 1)
WordBasic.SortArray arrWords
lngIndex = 0
For Each oPara In oRng.Paragraphs
oPara.Range.Words(1) = StrReverse(arrWords(lngIndex))
lngIndex = lngIndex + 1
Next oPara
lbl_Exit:
Exit Sub
End Sub
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.