Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Unique Records by Field

I have a list with 9 fields. There are three fields --
Last, Date, and Code -- that are important. Many of these
fields repeat -- e.g. Peters, 04/05/2003, 2 -- but when
they are unique I would like to place a 1 in a fourth
field called Unique. Thus if the above data is repeated
over 10 rows -- there would be one entry which has a 1 and
the others would have a blank or 0. If the 11th record was
Hobbs, 04/05/2003, 2 then there would be a 1 in the Unique
field as at least one field has changed.

I have done this with Advanced filter. But does someone
know a way to do it through code?

Thanks for any help.

Indu
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default Unique Records by Field

Suppose the three fields are columns B-D (2-4) starting
in row2. Then the following code will put a 1 in col A
when the next row doesn't match. (It assumes duplicates
are contiguous, not scattered.)

HTH,
Merjet

Sub macro1()
Dim iRow As Integer
Dim iCt As Integer
Dim ws As Worksheet

Set ws = Sheets("Sheet1")
iRow = ws.Range("B65536").End(xlUp).Row
ws.Range("A2:A" & iRow).Clear
ws.Range("A2") = 1
For iCt = 3 To iRow
If ws.Cells(iCt, 2) < ws.Cells(iCt - 1, 2) Or _
ws.Cells(iCt, 3) < ws.Cells(iCt - 1, 3) Or _
ws.Cells(iCt, 4) < ws.Cells(iCt - 1, 4) Then _
ws.Cells(iCt, 1) = 1
Next iCt
End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Unique Records by Field

Merjet and Troy, thank you for your help. I really
appreciate your solutions.

-----Original Message-----
Suppose the three fields are columns B-D (2-4) starting
in row2. Then the following code will put a 1 in col A
when the next row doesn't match. (It assumes duplicates
are contiguous, not scattered.)

HTH,
Merjet

Sub macro1()
Dim iRow As Integer
Dim iCt As Integer
Dim ws As Worksheet

Set ws = Sheets("Sheet1")
iRow = ws.Range("B65536").End(xlUp).Row
ws.Range("A2:A" & iRow).Clear
ws.Range("A2") = 1
For iCt = 3 To iRow
If ws.Cells(iCt, 2) < ws.Cells(iCt - 1, 2) Or _
ws.Cells(iCt, 3) < ws.Cells(iCt - 1, 3) Or _
ws.Cells(iCt, 4) < ws.Cells(iCt - 1, 4) Then

_
ws.Cells(iCt, 1) = 1
Next iCt
End Sub


.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Unique Records by Field

Another possibility. If you want to add an intermediate calculation you can
accomplish the desired result with a formula. Cell E1 will calculate = 1,
cell E2 will calculate = 0. If you have errors in any of the cells, then a
more robust formula would be needed.

Troy

A1: Peters
B1: 04/05/2003
C1: 2
D1: =A1&B1&C1
E1: =IF(COUNTIF(D$1:D1,D1)=1,1,0)

A2: Peters
B2: 04/05/2003
C2: 2
D2: =A2&B2&C2
E2: =IF(COUNTIF(D$1:D2,D2)=1,1,0)


"Indu Aronson" wrote in message
...
I have a list with 9 fields. There are three fields --
Last, Date, and Code -- that are important. Many of these
fields repeat -- e.g. Peters, 04/05/2003, 2 -- but when
they are unique I would like to place a 1 in a fourth
field called Unique. Thus if the above data is repeated
over 10 rows -- there would be one entry which has a 1 and
the others would have a blank or 0. If the 11th record was
Hobbs, 04/05/2003, 2 then there would be a 1 in the Unique
field as at least one field has changed.

I have done this with Advanced filter. But does someone
know a way to do it through code?

Thanks for any help.

Indu



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Unique Records by Field

Here is a formula that doesn't require the intermediate calculation.

You will need to adjust:
$A$1, $B$1, $C$1 values in the formula to reflect the top row of your
range.
$A$9, $B$9, $C$9 values in the formula to reflect the bottom row of your
range.

Your choice on which formula is easier to maintain.

Troy

(watch for word wrap)

D1:
=IF(SUMPRODUCT((A1:$A$9=A1)*(B1:$B$9=B1)*(C1:$C$9= C1))=SUMPRODUCT(($A$1:$A$9
=A1)*($B$1:$B$9=B1)*($C$1:$C$9=C1)),1,0)


"TroyW" wrote in message
...
Another possibility. If you want to add an intermediate calculation you

can
accomplish the desired result with a formula. Cell E1 will calculate = 1,
cell E2 will calculate = 0. If you have errors in any of the cells, then a
more robust formula would be needed.

Troy

A1: Peters
B1: 04/05/2003
C1: 2
D1: =A1&B1&C1
E1: =IF(COUNTIF(D$1:D1,D1)=1,1,0)

A2: Peters
B2: 04/05/2003
C2: 2
D2: =A2&B2&C2
E2: =IF(COUNTIF(D$1:D2,D2)=1,1,0)


"Indu Aronson" wrote in message
...
I have a list with 9 fields. There are three fields --
Last, Date, and Code -- that are important. Many of these
fields repeat -- e.g. Peters, 04/05/2003, 2 -- but when
they are unique I would like to place a 1 in a fourth
field called Unique. Thus if the above data is repeated
over 10 rows -- there would be one entry which has a 1 and
the others would have a blank or 0. If the 11th record was
Hobbs, 04/05/2003, 2 then there would be a 1 in the Unique
field as at least one field has changed.

I have done this with Advanced filter. But does someone
know a way to do it through code?

Thanks for any help.

Indu







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
Filer for unique records and return all column data for unique rec bseeley Excel Discussion (Misc queries) 1 September 12th 09 12:17 AM
Counting unique records in a field Loris Excel Discussion (Misc queries) 14 November 30th 08 04:55 PM
Unique Records Help pivot table Excel Worksheet Functions 2 August 23rd 08 12:12 AM
unique filter results in some non-unique records. Serials Librarian Excel Discussion (Misc queries) 2 May 26th 06 09:58 PM


All times are GMT +1. The time now is 01:48 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"