Registering and logging in removes this ad.
Registering and logging in removes this ad.
Command to Sort Clipboard

'
' this command will sort the text of a clipboard into alphabetical line order.
' without regard to spaces or tabs at the beginning or end of the line
'
Option Explicit
Sub Main
Dim a() As String, s As String, y As Long, x As Long, c As String, strLine As String
s = Clipboard()
y = 0
For x = 1 To Len( s )
c = Mid( s, x, 1 )
' Debug.Print x & ":" & Asc( c ) & ":" & c
Select Case c
Case vbNewLine, Chr( 13 ), Chr(10)
If Len( strLine ) > 0 Then
ReDim Preserve a(y)
a(y) = strLine
strLine = ""
y = y + 1
End If
Case Else
strLine = strLine & c
End Select
Next
If Len( strLine ) > 0 Then
ReDim Preserve a(y) ' line at the end with no cr/lf
a(y) = strLine
strLine = ""
y = y + 1
End If
If y > 0 Then
a = ListSort( a )
s = ""
For x = 0 To y-1
Debug.Print x & ":" & a(x)
s = s & a(x)
If x < y-1 Then
s = s & vbNewLine
End If
Next
Clipboard( s )
End If
End Sub
Function TrimAll( s As String ) As String
TrimAll = Replace( s, vbTab, "" )
TrimAll = Replace( TrimAll, " ", "" )
End Function
Function ListSort ( theList As Variant ) As Variant
' This routine sorts the elements in the list passed as a parameter
' Author: Geoff Weatherhead
' Date: 28/07/1998
Dim i, j, k As Integer
Dim temp As String
i = UBound( theList )
' For Each e In theList
' i = i + 1
' Next
If i > 0 Then
For j = 0 To i-1
For k = j To i
If LCase$ ( TrimAll( theList ( j ) ) ) > LCase$ ( TrimAll( theList ( k ) ) ) Then
temp = theList ( k )
theList ( k ) = theList ( j )
theList ( j ) = temp
End If
Next k
Next j
End If
ListSort = theList
End Function


