Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default countif - unique values only

I think there is a way to do this but having a brain fart at the moment. I have 3 columns, say A-B-C. First 2 are part types and the 3rd list tools. Certain tools go with certain parts indicated by an X in column A or B or both. The tools can be listed multiple times, even for the same part type. I would like to know how many unique tools belong to each part. If I use countif it returns all tools used for a certain part including duplicates. Is there a way to count only unique tools with no duplicates? thanks in advance..
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default countif - unique values only

On Monday, September 10, 2012 9:44:29 AM UTC-4, wrote:
I think there is a way to do this but having a brain fart at the moment. I have 3 columns, say A-B-C. First 2 are part types and the 3rd list tools. Certain tools go with certain parts indicated by an X in column A or B or both. The tools can be listed multiple times, even for the same part type. I would like to know how many unique tools belong to each part. If I use countif it returns all tools used for a certain part including duplicates. Is there a way to count only unique tools with no duplicates? The tools are identified with a 4 digit number ie. 1204. thanks in advance.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default countif - unique values only

Short of adding columns to the side of the data or using Data Validation to remove duplicates, I could only solve this one using a User-Defined Function (UDF). The UDF I wrote takes two arguments. Range1 is the range containing x's (column A in your example). Range2 is the range containing tools (column C in your example). The code will first check that the ranges used are only one column each and contain the same number of cells. Then, it will use a counter to run through each cell in both ranges tabulating the unique values. Since this code will run on the entire range of cells entered, I would avoid feeding it an entire column of data. The code will run much quicker if a specific range is used (even a large one of 50,000 cells is noticably quicker than an entire column).

To use this function, paste the code to a new module and enter the formula into a cell on your sheet. For example, if you wanted to count the unique number of tools (column C) for part 1 (column A), you would enter:

=UniqueCount(A2:A5000,C2:C5000)

Hope this helps,

Ben

Function UniqueCount(Range1 As Range, Range2 As Range)
Dim l As Long
Dim lCount As Long

lCount = 0

If Range1.Count < Range2.Count Then
UniqueCount = "Range sizes must be same size"
Exit Function
ElseIf Range1.Columns.Count + Range2.Columns.Count < 2 Then
UniqueCount = "Ranges must contain only one column each"
Exit Function
End If

For l = 1 To Range1.Count

If Range1.Range("A" & l).Value < "x" Then
lCount = lCount + 0
Else
If WorksheetFunction.CountIfs(Range1.Range("A1:A" & l), "x", _
Range2.Range("A1:A" & l), Range2.Range("A" & l)) 1 Then
lCount = lCount + 0
Else
lCount = lCount + 1
End If
End If

Next l

UniqueCount = lCount

End Function
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default countif - unique values only

On Monday, September 10, 2012 1:30:21 PM UTC-4, (unknown) wrote:
Short of adding columns to the side of the data or using Data Validation to remove duplicates, I could only solve this one using a User-Defined Function (UDF). The UDF I wrote takes two arguments. Range1 is the range containing x's (column A in your example). Range2 is the range containing tools (column C in your example). The code will first check that the ranges used are only one column each and contain the same number of cells. Then, it will use a counter to run through each cell in both ranges tabulating the unique values. Since this code will run on the entire range of cells entered, I would avoid feeding it an entire column of data. The code will run much quicker if a specific range is used (even a large one of 50,000 cells is noticably quicker than an entire column).



To use this function, paste the code to a new module and enter the formula into a cell on your sheet. For example, if you wanted to count the unique number of tools (column C) for part 1 (column A), you would enter:



=UniqueCount(A2:A5000,C2:C5000)



Hope this helps,



Ben



Function UniqueCount(Range1 As Range, Range2 As Range)

Dim l As Long

Dim lCount As Long



lCount = 0



If Range1.Count < Range2.Count Then

UniqueCount = "Range sizes must be same size"

Exit Function

ElseIf Range1.Columns.Count + Range2.Columns.Count < 2 Then

UniqueCount = "Ranges must contain only one column each"

Exit Function

End If



For l = 1 To Range1.Count



If Range1.Range("A" & l).Value < "x" Then

lCount = lCount + 0

Else

If WorksheetFunction.CountIfs(Range1.Range("A1:A" & l), "x", _

Range2.Range("A1:A" & l), Range2.Range("A" & l)) 1 Then

lCount = lCount + 0

Else

lCount = lCount + 1

End If

End If



Next l



UniqueCount = lCount



End Function


thanks for the code...it seems to work but can you adjust it to ignore blank cells?
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default countif - unique values only

thanks for the code...it seems to work but can you adjust it to ignore blank cells?

Sure thing, try this:

Function UniqueCount(Range1 As Range, Range2 As Range)
Dim l As Long
Dim lCount As Long

lCount = 0

If Range1.Count < Range2.Count Then
UniqueCount = "Range sizes must be same size"
Exit Function
ElseIf Range1.Columns.Count + Range2.Columns.Count < 2 Then
UniqueCount = "Ranges must contain only one column each"
Exit Function
End If
Debug.Print lCount
For l = 1 To Range1.Count

If Range1.Range("A" & l).Value < "x" Then
lCount = lCount + 0
Else
If Range2.Range("A" & l) = "" Then
lCount = lCount + 0
ElseIf WorksheetFunction.CountIfs(Range1.Range("A1:A" & l), "x", _
Range2.Range("A1:A" & l), Range2.Range("A" & l)) 1 Then
lCount = lCount + 0
Else
lCount = lCount + 1
End If
End If
Debug.Print lCount & ": " & Range1.Range("A1:A" & l).Address
Next l

UniqueCount = lCount

End Function
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default countif - unique values only

On Mon, 10 Sep 2012 13:07:19 -0700 (PDT), wrote:

On Monday, September 10, 2012 3:50:06 PM UTC-4, Ron Rosenfeld wrote:
On Mon, 10 Sep 2012 06:44:29 -0700 (PDT),
wrote:



I think there is a way to do this but having a brain fart at the moment. I have 3 columns, say A-B-C. First 2 are part types and the 3rd list tools. Certain tools go with certain parts indicated by an X in column A or B or both. The tools can be listed multiple times, even for the same part type. I would like to know how many unique tools belong to each part. If I use countif it returns all tools used for a certain part including duplicates. Is there a way to count only unique tools with no duplicates? thanks in advance.




Your data setup is unclear to me. If you have an "X" in column A or Column B, or both, how do you determine the part type to which this refers? Perhaps some examples of various lines would be of value.


the same tool can be used for both part types indicated by an "X" in column A and B. In another case, I may have a tool only used for one part type indicated by an "X" in the appropriate column. However in some cases the tool is listed multiple times. I would like to count how many unique tools I have for each part type. thanks.


I am still not understanding.

This is your set up as I understand it: X in A, X in B Tool in c

A B C
1 x x tool 1
2 X tool 2
3 x tool 3
4 x x tool 3

What are the part types?

Oh well, I see that you have an answer from benmclave, so he must be able to understand your layout.

Unless a part number is defined by the pattern of the x's, in which case there could be only 3 or four part numbers, which doesn't seem to make sense.
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default countif - unique values only

On Monday, September 10, 2012 9:00:09 PM UTC-4, Ron Rosenfeld wrote:
On Mon, 10 Sep 2012 13:07:19 -0700 (PDT), wrote:



On Monday, September 10, 2012 3:50:06 PM UTC-4, Ron Rosenfeld wrote:


On Mon, 10 Sep 2012 06:44:29 -0700 (PDT),
wrote:







I think there is a way to do this but having a brain fart at the moment. I have 3 columns, say A-B-C. First 2 are part types and the 3rd list tools. Certain tools go with certain parts indicated by an X in column A or B or both. The tools can be listed multiple times, even for the same part type. I would like to know how many unique tools belong to each part. If I use countif it returns all tools used for a certain part including duplicates.. Is there a way to count only unique tools with no duplicates? thanks in advance.








Your data setup is unclear to me. If you have an "X" in column A or Column B, or both, how do you determine the part type to which this refers? Perhaps some examples of various lines would be of value.




the same tool can be used for both part types indicated by an "X" in column A and B. In another case, I may have a tool only used for one part type indicated by an "X" in the appropriate column. However in some cases the tool is listed multiple times. I would like to count how many unique tools I have for each part type. thanks.




I am still not understanding.



This is your set up as I understand it: X in A, X in B Tool in c



A B C

1 x x tool 1

2 X tool 2

3 x tool 3

4 x x tool 3



What are the part types?



Oh well, I see that you have an answer from benmclave, so he must be able to understand your layout.



Unless a part number is defined by the pattern of the x's, in which case there could be only 3 or four part numbers, which doesn't seem to make sense.


the part numbers are at the top of the column. lets call it part A and part B. on your layout part A requires tools 1,2 and 3 and part B requires tool 1,3
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default countif - unique values only

On Mon, 10 Sep 2012 19:23:30 -0700 (PDT), wrote:

I would like to know how many unique tools belong to each part.

....
the part numbers are at the top of the column. lets call it part A and part B. on your layout part A requires tools 1,2 and 3 and part B requires tool 1,3


Given your stated setup so far, you can do this with a formula (adapted from
http://www.cpearson.com/Excel/Duplicates.aspx )

I Defined some names to use in these formulas:

Tools = $C$2:$C$n (the list of tools)

Part1 = the same range as tools but referring to column A
Part2 = the same range as tools but referring to column B.

I actually used a dynamic reference to name them, assuming there are no blanks in the "Tools" column

Tools =OFFSET(Sheet2!$C$1,1,0,COUNTA(Sheet2!$C:$C)-1)
Part1 =OFFSET(Tools,0,-2)
Part2 =OFFSET(Tools,0,-1)

This formula must be **array-entered**:

=SUM(IF(FREQUENCY(IF(LEN(IF(Part1="x",Tools,""))0 ,MATCH(IF(Part1="x",Tools,""),
IF(Part1="x",Tools,""),0),""), IF(LEN(IF(Part1="x",Tools,""))0,MATCH(IF(Part1="x ",Tools,""),
IF(Part1="x",Tools,""),0),""))0,1))

----------------------------------------

To **array-enter** a formula, after entering
the formula into the cell or formula bar, hold down
<ctrl<shift while hitting <enter. If you did this
correctly, Excel will place braces {...} around the formula.
  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default countif - unique values only

On Monday, September 10, 2012 10:58:22 PM UTC-4, Ron Rosenfeld wrote:
On Mon, 10 Sep 2012 19:23:30 -0700 (PDT), wrote:



I would like to know how many unique tools belong to each part.


...

the part numbers are at the top of the column. lets call it part A and part B. on your layout part A requires tools 1,2 and 3 and part B requires tool 1,3




Given your stated setup so far, you can do this with a formula (adapted from
http://www.cpearson.com/Excel/Duplicates.aspx )



I Defined some names to use in these formulas:



Tools = $C$2:$C$n (the list of tools)



Part1 = the same range as tools but referring to column A

Part2 = the same range as tools but referring to column B.



I actually used a dynamic reference to name them, assuming there are no blanks in the "Tools" column



Tools =OFFSET(Sheet2!$C$1,1,0,COUNTA(Sheet2!$C:$C)-1)

Part1 =OFFSET(Tools,0,-2)

Part2 =OFFSET(Tools,0,-1)



This formula must be **array-entered**:



=SUM(IF(FREQUENCY(IF(LEN(IF(Part1="x",Tools,""))0 ,MATCH(IF(Part1="x",Tools,""),

IF(Part1="x",Tools,""),0),""), IF(LEN(IF(Part1="x",Tools,""))0,MATCH(IF(Part1="x ",Tools,""),

IF(Part1="x",Tools,""),0),""))0,1))



----------------------------------------



To **array-enter** a formula, after entering

the formula into the cell or formula bar, hold down

<ctrl<shift while hitting <enter. If you did this

correctly, Excel will place braces {...} around the formula.


can you revise the formula to work if I have blanks in the tools column as I do have a case where there may be an "x" in the parts column and not necessarily a tool
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
Unique Values, not Unique Records steph44haf Excel Discussion (Misc queries) 1 May 12th 10 07:52 PM
COUNTIF: unique names and values accross several columns. SW Excel Worksheet Functions 2 July 5th 08 03:29 AM
Why does this code remove Duplicate Values, by showing only 1, but it does NOT show Unique values for some reason ? Corey Excel Programming 4 February 23rd 07 02:00 AM
Countif unique Bruce Excel Worksheet Functions 3 March 1st 06 11:06 AM
How do I search thr'o column and put unique values in differnt sheet and sum corresponding values in test test Excel Programming 3 September 9th 03 08:53 PM


All times are GMT +1. The time now is 07:27 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"