PC Review


Reply
Thread Tools Rate Thread

How can excel generate every unique pair from a set of numbers?

 
 
mistermat
Guest
Posts: n/a
 
      31st Aug 2008
If I have a set of unique numbers, for example:

1
2
3
4

how can i use excel to generate every unique pair automatically in two
separate columns, for example:

1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3

 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      31st Aug 2008
Hi,

This assumes you numbers are in column A and it will output to columns B & C

Right click your sheet tab, view code and past this in and run it

Sub stantial()
Range("B1:C" & Cells(Rows.Count, "B").End(xlUp).Row).ClearContents
myrow = 1
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For x = 1 To lastrow
For y = x + 1 To lastrow
Cells(myrow, 2).Value = Cells(x, 1).Value
Cells(myrow, 3).Value = Cells(y, 1).Value
myrow = myrow + 1
Cells(myrow, 2).Value = Cells(y, 1).Value
Cells(myrow, 3).Value = Cells(x, 1).Value
myrow = myrow + 1
Next
Next
Range("B1:C" & Cells(Rows.Count, "B").End(xlUp).Row).Select
Selection.Sort Key1:=Range("B1"), Order1:=xlAscending
End Sub

Mike

"mistermat" wrote:

> If I have a set of unique numbers, for example:
>
> 1
> 2
> 3
> 4
>
> how can i use excel to generate every unique pair automatically in two
> separate columns, for example:
>
> 1 2
> 1 3
> 1 4
> 2 1
> 2 3
> 2 4
> 3 1
> 3 2
> 3 4
> 4 1
> 4 2
> 4 3
>

 
Reply With Quote
 
mistermat
Guest
Posts: n/a
 
      31st Aug 2008
Mike, this is perfect. Thank you so much, I really appreciate your help!

"Mike H" wrote:

> Hi,
>
> This assumes you numbers are in column A and it will output to columns B & C
>
> Right click your sheet tab, view code and past this in and run it
>
> Sub stantial()
> Range("B1:C" & Cells(Rows.Count, "B").End(xlUp).Row).ClearContents
> myrow = 1
> lastrow = Cells(Rows.Count, "A").End(xlUp).Row
> For x = 1 To lastrow
> For y = x + 1 To lastrow
> Cells(myrow, 2).Value = Cells(x, 1).Value
> Cells(myrow, 3).Value = Cells(y, 1).Value
> myrow = myrow + 1
> Cells(myrow, 2).Value = Cells(y, 1).Value
> Cells(myrow, 3).Value = Cells(x, 1).Value
> myrow = myrow + 1
> Next
> Next
> Range("B1:C" & Cells(Rows.Count, "B").End(xlUp).Row).Select
> Selection.Sort Key1:=Range("B1"), Order1:=xlAscending
> End Sub
>
> Mike
>
> "mistermat" wrote:
>
> > If I have a set of unique numbers, for example:
> >
> > 1
> > 2
> > 3
> > 4
> >
> > how can i use excel to generate every unique pair automatically in two
> > separate columns, for example:
> >
> > 1 2
> > 1 3
> > 1 4
> > 2 1
> > 2 3
> > 2 4
> > 3 1
> > 3 2
> > 3 4
> > 4 1
> > 4 2
> > 4 3
> >

 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      31st Aug 2008
Glad I could help

"mistermat" wrote:

> Mike, this is perfect. Thank you so much, I really appreciate your help!
>
> "Mike H" wrote:
>
> > Hi,
> >
> > This assumes you numbers are in column A and it will output to columns B & C
> >
> > Right click your sheet tab, view code and past this in and run it
> >
> > Sub stantial()
> > Range("B1:C" & Cells(Rows.Count, "B").End(xlUp).Row).ClearContents
> > myrow = 1
> > lastrow = Cells(Rows.Count, "A").End(xlUp).Row
> > For x = 1 To lastrow
> > For y = x + 1 To lastrow
> > Cells(myrow, 2).Value = Cells(x, 1).Value
> > Cells(myrow, 3).Value = Cells(y, 1).Value
> > myrow = myrow + 1
> > Cells(myrow, 2).Value = Cells(y, 1).Value
> > Cells(myrow, 3).Value = Cells(x, 1).Value
> > myrow = myrow + 1
> > Next
> > Next
> > Range("B1:C" & Cells(Rows.Count, "B").End(xlUp).Row).Select
> > Selection.Sort Key1:=Range("B1"), Order1:=xlAscending
> > End Sub
> >
> > Mike
> >
> > "mistermat" wrote:
> >
> > > If I have a set of unique numbers, for example:
> > >
> > > 1
> > > 2
> > > 3
> > > 4
> > >
> > > how can i use excel to generate every unique pair automatically in two
> > > separate columns, for example:
> > >
> > > 1 2
> > > 1 3
> > > 1 4
> > > 2 1
> > > 2 3
> > > 2 4
> > > 3 1
> > > 3 2
> > > 3 4
> > > 4 1
> > > 4 2
> > > 4 3
> > >

 
Reply With Quote
 
Member
Join Date: Aug 2008
Posts: 45
 
      31st Aug 2008
If your numbers are in A1 and downward, putting this formula in B1 and dragging down will work.
=INDEX(A:A,MOD(INT((ROW()-1)/COUNT(A:A)),COUNT(A:A))+1,1) &","& INDEX(A:A,MOD(ROW()-1,COUNTA(A:A))+1,1)
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
automatically generate unique serial numbers for invoices nhlanhla Microsoft Excel Worksheet Functions 1 2nd Oct 2008 12:50 PM
how to generate series of random numbers which are all unique jyoti Microsoft Excel Programming 1 17th Apr 2008 11:07 AM
How to generate unique MRV Numbers? =?Utf-8?B?TGVzbGll?= Microsoft Access 0 16th Mar 2007 05:14 PM
Need to generate unique serial numbers using algorithm Alseikhan Microsoft Excel Programming 2 7th Apr 2006 08:42 AM
generate unique random numbers Stephen Larivee Microsoft Excel New Users 7 29th Mar 2006 01:04 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:09 PM.