Counting uniique items with criteria

Blunder1

Active Member
Joined
Jun 2, 2010
Messages
250
Hi,

Any help would be appreciated on this as i want to move away from functions.

Basically, i would like to add vba code, simialr to the countif one below, that will count the number of unique items in column B on sheets 2 (named wt) if the the cell value in columns c is equal to the cell value in sheets 1 (named ws) cell b6 (these are text)

Code:
ws.Range("d7") = WorksheetFunction.CountIf(wt.Range("D:D"), ws.Range("b6").Value)
Then in another cell on sheet 1 i would like to know what the highest unique item is ( i.e if Dog is the most frequent then the cell value in sheets 1 cell d8 = Dog)..

Thanks in advance

Blunder1
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi,

Assuming your data in Sheet2 in B1:C100 maybe this

Code:
Set ws = Sheets("Sheet1")
 
'Counting Unique in Sheet2!B1:B100 if Sheet2!C1:C100 = Sheet1!B6
ws.Range("D7") = Evaluate("=SUM(IF(FREQUENCY(IF(Sheet2!B1:B100<>"""",IF(Sheet2!C1:C100=Sheet1!B6," _
    & "MATCH(""~""&Sheet2!B1:B100,Sheet2!B1:B100&"""",0))),ROW(B1:B20)-ROW(B1)+1),1))")
 
'Highest count in Sheet2!C1:C100
ws.Range("D8") = Evaluate("=INDEX(Sheet2!C1:C100,MATCH(1,IF(COUNTIF(Sheet2!C1:C100,Sheet2!C1:C100)" _
    & "=MAX(COUNTIF(Sheet2!C1:C100,Sheet2!C1:C100)),1),0))")

HTH

M.
 
Upvote 0
oops...There is a typo in the first formula

The correct is
Code:
ws.Range("D7") = Evaluate("=SUM(IF(FREQUENCY(IF(Sheet2!B1:B100<>"""",IF(Sheet2!C1:C100=Sheet1!B6," _
        & "MATCH(""~""&Sheet2!B1:B100,Sheet2!B1:B100&"""",0))),ROW(B1:B100)-ROW(B1)+1),1))")

M.
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,242
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top