Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
HL8 HL8 is offline
Junior Member
 
Posts: 2
Default Permutation/Combination/Pairs order unimportant.

Hi,
I'm trying to take a list of authors and create pairs from them. The order in which they appear in a pair doesn't matter and I don't want duplicates of these pairs (I need them to generate coauthorship data). So, I have:

A
B
C
D

And want to generate:
AB
AC
AD
BC
BD
CD

This leaves out matches like "BA" since I already have "AB" and the order is unimportant to me. I've used the code below to create the full list of matches, but the output *does* include both "BA" and "AB".

Sub Permutations_2()
Dim rRng As Range
Dim lRow As Long, j As Long, k As Long

Set rRng = Range("A1", Range("A1").End(xlDown)) ' The set of values

For j = 1 To rRng.Count
For k = 1 To rRng.Count
If j < k Then
lRow = lRow + 1
Range("C" & lRow) = Range("a" & j)
Range("D" & lRow) = Range("a" & k)
End If
Next k
Next j
End Sub

Any ideas on how to eliminate the duplicates - either in the macro or in an additional step?

Many thanks,
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 829
Default Permutation/Combination/Pairs order unimportant.

"HL8" wrote:
Sub Permutations_2()
Dim rRng As Range
Dim lRow As Long, j As Long, k As Long
Set rRng = Range("A1", Range("A1").End(xlDown)) ' The set of values
For j = 1 To rRng.Count
For k = 1 To rRng.Count
If j < k Then
lRow = lRow + 1
Range("C" & lRow) = Range("a" & j)
Range("D" & lRow) = Range("a" & k)
End If
Next k
Next j
End Sub

Any ideas on how to eliminate the duplicates -
either in the macro or in an additional step?


For j = 1 to rRng.Count-1
For k = j + 1 to rRng.Count
lRow = lRow + 1
Range("C" & lRow) = Range("a" & j)
Range("D" & lRow) = Range("a" & k)
Next
Next

More efficient:

Dim v As Variant
For j = 1 to rRng.Count-1
v = Range("a" & j)
For k = j + 1 to rRng.Count
lRow = lRow + 1
Range("C" & lRow) = v
Range("D" & lRow) = Range("a" & k)
Next
Next
  #3   Report Post  
HL8 HL8 is offline
Junior Member
 
Posts: 2
Default

This worked perfectly. Thanks for the help!


Quote:
Originally Posted by joeu2004[_2_] View Post
"HL8" wrote:
Sub Permutations_2()
Dim rRng As Range
Dim lRow As Long, j As Long, k As Long
Set rRng = Range("A1", Range("A1").End(xlDown)) ' The set of values
For j = 1 To rRng.Count
For k = 1 To rRng.Count
If j < k Then
lRow = lRow + 1
Range("C" & lRow) = Range("a" & j)
Range("D" & lRow) = Range("a" & k)
End If
Next k
Next j
End Sub

Any ideas on how to eliminate the duplicates -
either in the macro or in an additional step?


For j = 1 to rRng.Count-1
For k = j + 1 to rRng.Count
lRow = lRow + 1
Range("C" & lRow) = Range("a" & j)
Range("D" & lRow) = Range("a" & k)
Next
Next

More efficient:

Dim v As Variant
For j = 1 to rRng.Count-1
v = Range("a" & j)
For k = j + 1 to rRng.Count
lRow = lRow + 1
Range("C" & lRow) = v
Range("D" & lRow) = Range("a" & k)
Next
Next
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
PERMUTATION AND COMBINATION FC Excel Discussion (Misc queries) 3 August 30th 07 04:14 AM
Permutation combination of a 4 digit no [email protected] Excel Discussion (Misc queries) 2 August 20th 07 07:19 PM
Permutation and combination vishu Excel Discussion (Misc queries) 2 May 19th 05 07:47 AM
Creating a Combination or Permutation Array in Excel D.L. Excel Programming 4 November 1st 04 05:32 PM
Combination/Permutation Generation Tom Ogilvy Excel Programming 0 September 9th 04 04:34 PM


All times are GMT +1. The time now is 09:26 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"