View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
Dave D-C[_3_] Dave D-C[_3_] is offline
external usenet poster
 
Posts: 176
Default A little variation of conventional sorting

I see that doing this almost demands using recursion.
This program assumes that the pairs are in Cols 1 and 2
and nothing is below the pairs. The group number goes
into column 3 and the group item lists goes below the pairs.
You could obviously sort the pairs by column 3 to get a
list of pairs instead of a list of items. D-C

Option Explicit
Dim gColl As New Collection, gRng As Range, gRowZ&

Sub Main()
Dim iRowV&, nGroup%
Set gRng = UsedRange ' original usedrange
' get last row
gRowZ = gRng.SpecialCells(xlCellTypeLastCell).Row
iRowV = 1
Do While iRowV <= gRowZ ' look for group start
If Cells(iRowV, 3) = "" Then
nGroup = nGroup + 1 ' is a start
Call Group1st(nGroup, iRowV)
End If
iRowV = iRowV + 1
Loop
End Sub

Sub Group1st(pGroup%, pRow&)
Dim iRow&
' do columns 1 and 2
Call GroupNth(pGroup, Cells(pRow, 1))
Call GroupNth(pGroup, Cells(pRow, 2))
' done, list the group items at the end
iRow = gRowZ + pGroup + 1
Cells(iRow, 1) = pGroup ' Group#
Do While gColl.Count 0 ' get items
Cells(iRow, gColl.Count + 2).Value = gColl(gColl.Count)
gColl.Remove gColl.Count ' and remove
Loop
End Sub

Sub GroupNth(pGroup%, Cell1 As Range)
Dim i1%, s1$, CellV As Range, zRngF As Range
Cells(Cell1.Row, 3) = pGroup ' group#
i1 = Cell1.Value
gColl.Add i1, Format(i1) ' add item
' 1st find
Set CellV = gRng.Find(i1, Cell1, xlFormulas, xlWhole)
s1 = CellV.Address
Do While CellV.Address < Cell1.Address
' if another find, do again recursively
' with adjacent cell
Call GroupNth(pGroup, Cells(CellV.Row, 3 - CellV.Column))
' can't do findnext with recursion
Set CellV = gRng.Find(i1, CellV, xlFormulas, xlWhole)
Loop
End Sub

Legend wrote:
Suppose that I have some data:
12,30
12,45
2,3
7,8
3,9
30, 8
45,54
56,65
Where (a,b) indicates that a is connected to b. I want to get all
connected nodes to one point. For instance, the output of the above
example should be something like:
Group 1
2,3
3,9
Group 2
12,30
12,45
30,8
7,8
Group 3
45,54
Group 4
56,65
The order is not important as long as the whole group stays together.
Reason why they are grouped like that:
1. 2 is connected to 3 and 3 is connected to 9 and so we put all the
three, i.e. 2,3,9 into one group.
2. 12 is connected to 45 and 12 is also connected to 30 so we put
these in the same group but 30 is connected to 8 and 8 is connected to
7 so ultimately we put all these into the same group.
3. 45 and 54 are connected but not related to any other numbers so we
put them into another group
4. 56 and 65 are connected but not related to any other numbers so we
put them into another group
I am unable to figure out an algorithm for this. Can someone guide me?


"Bill Renaud" wrote:
[good stuff]

Legend wrote:
Actually the first option looks good though I still want to preserve
the parent-child relation here. For example, from the above list, I
want to retain the least information that when 2,3 and 3,9 belong to
the group, 2 and 3 are directly related; 3 and 9 are directly related
instead of falling into the assumption that 2 and 9 are directly
related. But either ways its fine... Would you suggest a workflow for
this please?




----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----