views:

278

answers:

5

This is for Excel: I've been tasked with counting distinct records after I have filtered the data. I have 330 rows with column A containing the 'name' and in Column B I have the name of a test that was done for each 'name', which each 'name' could have taken several iterations of the same test. The test results are in Column C.

Col A -Student  Col B -Exam          Col C - Grade
Student 1       Exam 1              .80
Student 2       Exam 1              .50
Student 3       Exam 1              .90
Student 2       Exam 1              .75
Student 4       Exam 1              .90
Student 5       Exam 1              .55
Student 2       Exam 2              .90
Student 1       Exam 2              .90
....            ....                ...

If I filter col B for Exam 1, I want to count the unique number of students that have taken Exam 1.

A: 

I'm not sure how column B is fitting in here, but...

select distinct column_A, count(*)
from table
where column_C = 'A+'
group by column_A
thursdaysgeek
A: 

In line with thursdaysgeek

strFile = Workbooks(1).FullName
''Note HDR=Yes, so column names can be used
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

Set cn = CreateObject("ADODB.Connection")
cn.Open strCon
Set rs = CreateObject("ADODB.Recordset")

strSQL = "SELECT DISTINCT Student, Exam FROM [Sheet4$] " _
       & "WHERE Exam='Exam 1'"

rs.Open strSQL, cn

For i = 0 To rs.Fields.Count - 1
    Sheets("Sheet5").Cells(1, i + 1) = rs.Fields(i).Name
Next

Sheets("Sheet5").Cells(2, 1).CopyFromRecordset rs
Remou
A: 
Dick Kusleika
What if I want to filter by grade/Col C? This is just a simplified example datasheet, the real spreadsheet has at least 50 columns of different data and if I filter on any of those columns, I need the distinct 'name' column to change and be able to provide a distinct 'name' count. I'm using the following to count a filtered column, but not sure if I can use a modified verson to determine distinct:=SUMPRODUCT(SUBTOTAL(9,OFFSET(U8:U331,ROW(U8:U331)-MIN(ROW(U8:U331)),,1)),--(U8:U331>=$I$3)). Appreciate any advice you can provide.
Deon
Your post did say "fitlered" didn't it? I don't know of a way with formulas, but edited my response with some VBA.
Dick Kusleika
Thank you! I went ahead and used the VBA code and it works very well.
Deon
A: 

I wouldn't use code at all here, I'd go for a Pivot Table

MikeD
A: 

Found this:

=SUMPRODUCT((A1:A30000<>"")/COUNTIF(A1:A30000,A1:A30000&""))

on the Excel Forum

Tested in on your example and .. it works :-)

Grendler