Detecting first occurence and marking others as repeat

vibhorkhanna

New Member
Joined
Mar 12, 2015
Messages
3
hi I am trying to find the first occurrence of a value within a set of values within bigger series and mark others as repeat values.
The table below might help understand better.
ID Unique/Repeat Supervisor Remark Count Expected Output
A 1 MO NC 7 MO NC
A 0 MO NC REPEAT REPEAT
A 0 RO C RO C
A 0 RO C REPEAT REPEAT
A 0 MO C REPEAT REPEAT
A 0 RO C REPEAT REPEAT
A 0 GO NC GO REPEAT
B 1 MO NC 2 MO NC
B 0 MO NC REPEAT REPEAT

I have made a function DetectChange, to achieve the above result:
=IF(B2=1,C2,DetectChange(MATCH(A2,A:A,0):MATCH(A2,A:A,0)+COUNTIF(A:A,A2),A2:MATCH(A2,A:A,0)+COUNTIF(A:A,A2),Column(C2))) ' for supervisor column
=IF(B2=1,D2,DetectChange(MATCH(A2,A:A,0):MATCH(A2,A:A,0)+COUNTIF(A:A,A2),A2:MATCH(A2,A:A,0)+COUNTIF(A:A,A2),Column(D2))) ' for remark column

Public Sub DetectChange(ByVal FullRange As Range, ByVal TestRange As Range, ByVal Pos As Integer)
Dim wrk, fnd, result As Range
Dim i, j As Integer

i = FullRange.Row.Count

For j = 0 To i Step 1
wrk(j) = FullRange(j).Offset(0, Pos - 1)
Next j

fnd = TestRange(0)
With Range(wrk)
Set result = .Find(What:=fnd, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
DetectChange = "REPEAT"
Else
DetectChange = fnd
End If
End With
End If
End Sub

I am still a novice at VBA coding and any help would be greatly appreciated. Many Thanks!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
This should be accomplishable with a formula, why VBA?
 
Last edited:
Upvote 0
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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