If Duplicate, copy info into other Duplicates

KGards7

New Member
Joined
Mar 31, 2022
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I need to assign a macro to a button which basically does the following:

If there is a duplicate in column A then whatever data is in columns S, T and U for the first entry is to copy to all duplicates.

E.g. if there are 5 cells in column A that each read "Test1" then whatever is in column S, T and U for the very first "Test1" needs to copy to all other "Test1"'s in the sheet.

Hope this makes sense. Thank you and again your help is and always will be greatly appreciated.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Sure, here's a VBA macro that should do what you need:
Sub CopyDataForDuplicates()
Dim lastRow As Long
Dim dict As Object
Dim i As Long
Dim key As Variant
Dim firstRow As Long
Dim sVal As String
Dim tVal As String
Dim uVal As String

'Get the last row of data in column A
lastRow = Range("A" & Rows.Count).End(xlUp).Row

'Create a dictionary object to store the first row number for each unique value in column A
Set dict = CreateObject("Scripting.Dictionary")
For i = 2 To lastRow
If Not dict.Exists(Range("A" & i).Value) Then
dict.Add Range("A" & i).Value, i
End If
Next i

'Loop through the dictionary and copy data as needed
For Each key In dict.Keys
firstRow = dict(key)
sVal = Range("S" & firstRow).Value
tVal = Range("T" & firstRow).Value
uVal = Range("U" & firstRow).Value
For i = firstRow + 1 To lastRow
If Range("A" & i).Value = key Then
Range("S" & i).Value = sVal
Range("T" & i).Value = tVal
Range("U" & i).Value = uVal
End If
Next i
Next key
End Sub

You can assign this macro to a button on your worksheet by right-clicking on the button, selecting "Assign Macro," and then choosing the name of this macro from the list.
 
Upvote 0
Solution
Sure, here's a VBA macro that should do what you need:
Sub CopyDataForDuplicates()
Dim lastRow As Long
Dim dict As Object
Dim i As Long
Dim key As Variant
Dim firstRow As Long
Dim sVal As String
Dim tVal As String
Dim uVal As String

'Get the last row of data in column A
lastRow = Range("A" & Rows.Count).End(xlUp).Row

'Create a dictionary object to store the first row number for each unique value in column A
Set dict = CreateObject("Scripting.Dictionary")
For i = 2 To lastRow
If Not dict.Exists(Range("A" & i).Value) Then
dict.Add Range("A" & i).Value, i
End If
Next i

'Loop through the dictionary and copy data as needed
For Each key In dict.Keys
firstRow = dict(key)
sVal = Range("S" & firstRow).Value
tVal = Range("T" & firstRow).Value
uVal = Range("U" & firstRow).Value
For i = firstRow + 1 To lastRow
If Range("A" & i).Value = key Then
Range("S" & i).Value = sVal
Range("T" & i).Value = tVal
Range("U" & i).Value = uVal
End If
Next i
Next key
End Sub

You can assign this macro to a button on your worksheet by right-clicking on the button, selecting "Assign Macro," and then choosing the name of this macro from the list.
Works Perfectly!!

Thank you!!!
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,487
Members
448,967
Latest member
visheshkotha

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