VBa - Quickest Way to put 0’s where these are duplicated

Sahil123

Well-known Member
Joined
Oct 31, 2011
Messages
673
Hi

i have a dataset where there could some duplicates

if the name and date matches then put a 0 in that row (only duplicated row)

eg
DateTime, Name, Location, Time, Time2, Subject, Time3

27/07/2020 09:00, Simon, London, 03:50:00, 01:00:00, English, 02:00:00

27/07/2020 09:00, Simon, Liverpool , 03:50:00, 01:00:00, English, 02:00:00

The data should in that example look like this


27/07/2020 09:00, Simon, London, 03:50:00, 01:00:00, English, 02:00:00

27/07/2020 09:00, Simon, Liverpool , 0, 0, English, 0

Hope this makes sense
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Ive seen this formula but that would mean creating additional columns and on a large dataset could take long so was hoping maybe an array solution or some other sort would be better

I2=IF(SUMPRODUCT(--($A$1:A1=A2),--($B$1:B1=B2)),0,E2)

J2= IF(SUMPRODUCT(--($A$1:A1=A2),--($B$1:B1=B2)),0,F2) etc

K2= IF(SUMPRODUCT(--($A$1:A1=A2),--($B$1:B1=B2)),0,H2) etc
 
Upvote 0
Hello,

Assuming this data I have here looks correct, I think this has the effect you're looking for.

Book1
ABCDEFG
1DateTimeNameLocationTimeTime2SubjectTime3
227/07/2020SimonLondon3:50:001:00:00English2:00:00
327/07/2021SimonLiverpool3:50:001:00:00English3:00:00
Sheet1


VBA Code:
Sub InsZeroes()

Dim c As Range, LR As Integer, ws1 As Worksheet

Set ws1 = ActiveSheet

LR = ws1.Cells(Rows.Count, "A").End(xlUp).Row

For Each c In ws1.Range("A2:A" & LR)
    If c.Value = c.Offset(1, 0).Value And c.Offset(0, 1).Value = c.Offset(1, 1).Value Then
        c.Offset(1, 3).Value = 0
        c.Offset(1, 3).NumberFormat = "General"
        c.Offset(1, 4).Value = 0
        c.Offset(1, 4).NumberFormat = "General"
        c.Offset(1, 6).Value = 0
        c.Offset(1, 6).NumberFormat = "General"
    End If
Next c

End Sub
 
Upvote 0
Hi

Thank you

that wont work as that would mean data is sorted

the data could come in an un sorted format
 
Upvote 0
How about
VBA Code:
Sub Sahil()
   Dim Cl As Range
   Dim Tmp As String
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         Tmp = Cl.Value2 & "|" & Cl.Offset(, 1).Value
         If Not .exists(Tmp) Then
            .Add Tmp, Nothing
         Else
            Cl.Offset(, 3).Resize(, 2).Value = 0
            Cl.Offset(, 6).Value = 0
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Nope, the code uses what's called late binding, so no need for references.
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,619
Members
449,039
Latest member
Mbone Mathonsi

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