MS Office Forum / Excel / New Users / December 2006
Counting Combinations within a Cell
|
|
Thread rating:  |
Tom G - 18 Dec 2006 09:35 GMT Hi,
In cells A1 and B1 I have the following data:
4,5,6,7 4,5,6,7
In B3 I want to count the number of non-matching combinations.
The answer is 12.
There are 16 total combinations, but 4 cannont be matched with 4, and 5 cannot be matched with 5, etc., which leaves 12.
I'm looking for one formula that can count the number of non-matching combinations.
Thanks,
Tom G
Tom G - 18 Dec 2006 10:37 GMT >Hi, > [quoted text clipped - 15 lines] > >Tom G No, the strings aren't always identical length. It can vary widely. Sometimes one will have three and the other one, or any amount up to twelve each max.
PapaDos - 18 Dec 2006 11:41 GMT Are the entries alway one character length ? Are the entries always unique ?
 Signature Regards, Luc.
"Festina Lente"
> >Hi, > > [quoted text clipped - 19 lines] > Sometimes one will have three and the other one, or any amount up to > twelve each max. Tom G - 18 Dec 2006 11:53 GMT >>Hi, >> [quoted text clipped - 19 lines] >Sometimes one will have three and the other one, or any amount up to >twelve each max. Are the entries alway one character length ? Are the entries always unique ?
 Signature Regards, Luc.
The entries will always be a number between 1 and 12, seperated by commas. I'm not sure what you mean by unique.
EX:
1,2,3 1,3,4,5,7,9
1,4 2,5,6,11
1 4,7,8,9
2,3 2,3,7,8,9
These are examples of how the cells might look. Sometimes there are matching values, and other times there are not.
Thanks,
Tom
PapaDos - 18 Dec 2006 12:33 GMT By "not" unique, I mean that it is possible that an entry is repeated more than once in a cell, like in (1,1,2,3). Is it a possible situation ?
 Signature Regards, Luc.
"Festina Lente"
> >>Hi, > >> [quoted text clipped - 22 lines] > Are the entries alway one character length ? > Are the entries always unique ? Tom G - 18 Dec 2006 18:18 GMT >By "not" unique, I mean that it is possible that an entry is repeated more >than once in a cell, like in (1,1,2,3). >Is it a possible situation ? OK, I see. No there will never be any repeats within a cell.
Tom
Tom G - 19 Dec 2006 16:09 GMT >>By "not" unique, I mean that it is possible that an entry is repeated more >>than once in a cell, like in (1,1,2,3). [quoted text clipped - 3 lines] > >Tom Anybody?
Dana DeLouis - 19 Dec 2006 17:05 GMT Hi. This doesn't have much error checking, but would this general idea work?
=CountUnique(A1,B1) 12
Function CountUnique(s1 As String, s2 As String) As Double Dim V1 As Variant Dim V2 As Variant Dim N As Double Dim j As Long Dim k As Long
'// Index is zero-based V1 = Split(s1, ",") V2 = Split(s2, ",")
'// Assume they are all unique N = (UBound(V1) + 1) * (UBound(V2) + 1) '// Subtract 1 if there is a match '// (Note: True is -1 in vba) For j = 0 To UBound(V1) For k = 0 To UBound(V2) N = N + (V1(j) = V2(k)) Next k Next j CountUnique = N End Function
 Signature HTH :>) Dana DeLouis Windows XP & Office 2003
>>>By "not" unique, I mean that it is possible that an entry is repeated >>>more [quoted text clipped - 6 lines] > > Anybody? Tom G - 19 Dec 2006 19:25 GMT >Hi. This doesn't have much error checking, but would this general idea >work? [quoted text clipped - 24 lines] > CountUnique = N >End Function Hi, do I just enter all of that into a cell?
Tom
Dana DeLouis - 19 Dec 2006 22:24 GMT > Hi, do I just enter all of that into a cell? Hi. No. It's a vba code. Here's what you do. From the worksheet, do Alt+F11. This brings up the vba editor. From the editor, do "Insert - Module" from the Main Menu. Paste the code here.
Function CountUnique(s1 As String, s2 As String) As Double Dim V1 As Variant Dim V2 As Variant ...etc CountUnique = N End Function
Now, back on your worksheet, enter the following into a cell:
=CountUnique(A1,B1)
Make sure A1 & B1 have strings similar to your example (ie "1,2,3,4") Don't hesitate to post back if you have questions, especially if it's your first macro. Again, this was a quick example to get you going.
 Signature HTH :>) Dana DeLouis Windows XP & Office 2003
>>Hi. This doesn't have much error checking, but would this general idea >>work? [quoted text clipped - 28 lines] > > Tom Tom G - 19 Dec 2006 23:44 GMT >> Hi, do I just enter all of that into a cell? > [quoted text clipped - 18 lines] >first macro. >Again, this was a quick example to get you going. This is a great work around for what I wanted. It worked great. I had go up a few posts and grab the whole code, becuse I got a syntax error, but I figured it out right away... and it is very slick.
Thanks a lot for your time..
Tom
Tom G - 20 Dec 2006 01:45 GMT >Hi. This doesn't have much error checking, but would this general idea >work? [quoted text clipped - 24 lines] > CountUnique = N >End Function Dana,
Could this same code be slightly changed for doing the same thing with three cells? And then four cells? That will complete my project.
In other words, I already have what I need for two cells, now I need to do the same thing for three and four cells.
EX: For three cells:
A6 B6 C6 2,3,4 4,5,6 2,3,4,5,6
EX: For four cells:
A10 B10 C10 D10 1,2 1,2,3 2,3,4,5 6,7,8,9,10
Thanks for your help.
Tom
Harlan Grove - 20 Dec 2006 18:39 GMT Tom G wrote... ...
>Could this same code be slightly changed for doing the same thing with >three cells? And then four cells? That will complete my project. [quoted text clipped - 11 lines] >A10 B10 C10 D10 >1,2 1,2,3 2,3,4,5 6,7,8,9,10 ...
It could, but there comes a point at which spreadsheets and procedural programming languages simply aren't the best tools for the task. Your task is equivalent to generating generalized crossproducts and counting up the tuples in which all entries are distinct values. There are other software packages that'd be much better at this.
However, if you must do this in Excel, you could try the following udf.
Function foo(ParamArray a() As Variant) As Long Dim i As Long, j As Long, k As Long Dim b As Variant, c As Variant, d As Variant Dim x As Variant, y As Variant
For Each x In a If Not IsArray(x) Then x = Array(x)
For Each y In x
If IsEmpty(b) Then b = Split(y, ",")
Else c = Split(y, ",") ReDim d(0 To (UBound(b) + 1) * (UBound(c) + 1) - 1) k = -1
For i = 0 To UBound(b)
For j = 0 To UBound(c)
If InStr(1, "," & b(i) & ",", "," & c(j) & ",") = 0 Then k = k + 1 d(k) = b(i) & "," & c(j) End If
Next j
Next i
ReDim Preserve d(0 To k) b = d Erase c Erase d
End If
Next y
Next x
foo = UBound(b) + 1
End Function
Frank Price - 21 Dec 2006 00:51 GMT >It could, but there comes a point at which spreadsheets and procedural >programming languages simply aren't the best tools for the task. Your [quoted text clipped - 8 lines] > Dim b As Variant, c As Variant, d As Variant > Dim x As Variant, y As Variant..... I understand. Thank you I'll try this.
Tom
Tom G - 19 Dec 2006 19:33 GMT Maybe this would be simpler.
A1 A2
3,4,5 3,4,5,6
The number of unique combinations is 9. (4*3)-3.
If you multiply the number of digits in each cell (4 and 3) together, and then subtract the number of matches, you have the number of combinations.
In the above example you have 4*3=12. The 3,4 and 5 are duplicated in both cells, so you would subtract the number of duplicatons (3) for an answer of 9.
Perhaps it would be easier to write a formula that way?
Tom
Tom G - 19 Dec 2006 20:53 GMT >Maybe this would be simpler. > [quoted text clipped - 15 lines] > >Tom OK, let's start over. All I need is a way to count matching numbers.
A1 A2
2,3,4 2,3,4,5
There are three matching numbers in the two cells. (2 3 and 4) Is there a formula to count those matches?
|
|
|