Sorting Text | CodeGuru

Sorting Text

Somtimes, I want to sort a block of text. So just select it and let the bubble sort Sub SortSelection () ‘DESCRIPTION: Sorts Selection Dim win set win = ActiveWindow If win.type “Text” Then MsgBox “This macro can only be run when a text editor window is active.” Exit Sub End If StartLine = ActiveDocument.Selection.TopLine […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 11, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Somtimes, I want to sort a block of text. So just select it and let the bubble sort

Sub SortSelection ()DESCRIPTION: Sorts Selection
    Dim win
    set win = ActiveWindow
    If win.type <>TextThen
        MsgBoxThis macro can only be run when a text editor window is active.Exit Sub
    End If
    StartLine = ActiveDocument.Selection.TopLine
    EndLine = ActiveDocument.Selection.BottomLine
    If EndLine < StartLine Then
        Temp = StartLine
        StartLine = EndLine
        EndLine = Temp
    End If

    EndLine = EndLine - 1

    If StartLine > EndLine Then
        Exit Sub
    End If
    bStop = false
    Do While Not bStop
        bStop = true
        For i = StartLine  To EndLine
            ActiveDocument.Selection.GoToLine i
            ActiveDocument.Selection.SelectLine
            FirstLine = ActiveDocument.Selection
            ActiveDocument.Selection.GoToLine i + 1
            ActiveDocument.Selection.SelectLine
            SecondLine = ActiveDocument.Selection
            If FirstLine > SecondLine Then
                bStop = false
                ActiveDocument.Selection.GoToLine i
	            ActiveDocument.Selection.StartOfLine
	            ActiveDocument.Selection.MoveTo i + 1, dsEndOfLine, dsExtend
                ActiveDocument.Selection = SecondLine +  FirstLine
	            ActiveDocument.Selection.EndOfLine
	            ActiveDocument.Selection.Delete
            End If
        Next
    Loop
    ActiveDocument.Selection.GoToLine StartLine
    ActiveDocument.Selection.StartOfLine
    ActiveDocument.Selection.MoveTo EndLine + 1, dsEndOfLine, dsExtend
End Sub

Date Last Updated: February 12, 1999

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.