Sort alpha in 1 column with gaps?

K

k1ngy

How do i sort a-z column 2 only. I can do it if i have a, b, c, d mixed in
column 2 but not if there are gaps, i have shown example below
Any help,
Thanks
A A A A
B D B B B
C C C C
D B D D D

Like this
A A A A
B B B B B
C C C C
D D D D D
 
G

Graham Mayor

It must be the heat? Something is causing correspondents to write gibberish
this morning. :(
Can you explain in English what it is you are trying to do?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Suzanne S. Barnhill

Think of it as a fruit machine ("slot machine" Stateside). He's got columns
with letters A-Z, and he wants to sort each column separately so that it is
in alpha order, but some of the columns have gaps between letters, and those
have to be placed where they belong (not rise to the top) in order to keep
the B with the other B's, etc. The only solution I can think of would be to
actually insert the appropriate letter and format it as white or possibly
Hidden.
 
G

Graham Mayor

That crystal ball again? ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Helmut Weber

Hi Suzanne,
hi Graham,

as there was no other amusement on a lazy sunday morning...

I wonder whether this will help the OP,
but is was a nice exercise:

Sub Test1()
Dim bEmp As Boolean ' is empty cell
Dim oTbl As Table
Dim oclm As Column
Dim oCll As Cell
Dim lngCll As Long
Dim cArr() As String
Set oTbl = Selection.Tables(1)
For Each oclm In oTbl.Columns
bEmp = False
oclm.Select
' is there an empty cell?
For Each oCll In Selection.Cells
If Len(oCll.Range.Text) = 2 Then
bEmp = True
Exit For
End If
Next
If bEmp = False Then
' no empty cell then just sort the column
Selection.Sort ExcludeHeader:=False, _
SortColumn:=True, _
sortorder:=wdSortOrderAscending
Else
' there is at least one empty cell
Selection.Sort ExcludeHeader:=False, _
SortColumn:=True, _
sortorder:=wdSortOrderAscending
lngCll = 0
' count not empty cells
For Each oCll In Selection.Cells
If Len(oCll.Range.Text) > 2 Then
lngCll = lngCll + 1
End If
Next
' set up an array for the values of the not empty cells
ReDim cArr(1 To lngCll)
lngCll = 0
' put the sorted values into the array
For Each oCll In Selection.Cells
If Len(oCll.Range.Text) > 2 Then
lngCll = lngCll + 1
cArr(lngCll) = _
Left(oCll.Range.Text, Len(oCll.Range.Text) - 2)
End If
Next
' unsort the column
ActiveDocument.Undo 1
lngCll = 0
' put the values from the array of not empty cells
' into the not empty cells
For Each oCll In Selection.Cells
If Len(oCll.Range.Text) > 2 Then
lngCll = lngCll + 1
oCll.Range.Text = cArr(lngCll)
End If
Next
End If
Next
End Sub

I've used Word's sort algorithm in order to avoid
having to include something like bubblesort
and thus start a discussion about sorting, possibly.

Have a nice day.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top