View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
AA2e72E[_2_] AA2e72E[_2_] is offline
external usenet poster
 
Posts: 93
Default select distinct row

Here is a solution using SQL.
1. Save your original workbook.
2. Create a new Workbook and paste this code in the ThisWorkbook module.

Sub Split()
Set ADORS = CreateObject("ADODB.RecordSet")
Cnn = "Provider=MSDASQL;Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\distinct.xls;"
Sql = "Select * from [Sheet1$]"
ADORS.Open Sql, Cnn ' All the data
AddColNames ADORS, 1
ADORS.Close
Sql = "select * from [Sheet1$] where name in (SELECT distinct(name)FROM [Sheet1$] group by name having count(name)= 1;)"
ADORS.Open Sql, Cnn ' Unique by Name
AddColNames ADORS, 2
ADORS.Close
Sql = "select * from [Sheet1$] where name in (SELECT distinct(name)FROM [Sheet1$] group by name having count(name) 1;)"
ADORS.Open Sql, Cnn ' Recurring by Name
AddColNames ADORS, 3
ADORS.Close
Set ADORS=Nothing
End Sub

Sub AddColNames(ByVal RS As Variant, ByVal SheetNo As Integer)
Col = 1
For Each fld In RS.Fields
ActiveWorkbook.Sheets(SheetNo).Cells(1, Col) = fld.Name
Col = Col + 1
Next
ActiveWorkbook.Sheets(SheetNo).Range("A2").CopyFro mRecordset RS
End Sub

3. Run the sub Split.

CA
a. Replace C:\distinct.xls with the location and name of your workbook in the Cnn string.
b. I have assumed that the original data is in Sheet1, that the unique data goes in Sheet2, and the recurring data goes in Sheet3: your new workbook must have at least 3 sheets.

You might be able to adapt the code to work within your original workbook: I don't know how this is structured.