
Originally Posted by
Raistlin8522
What I am having trouble with now is that sometimes the script is causing the records to be duplicated several times. I am not sure why this is happening.
Because the code is written based on the assumption that NO duplicate within a sheet.
If duplicate within a sheet, take only the first data.
If not, you need to show me your desired result with the duplicates.
Sub test()
Dim a, i As Long, ii As Long, w
With Sheets("mastersheet").ListObjects("Table1")
a = .DataBodyRange.Value
.DataBodyRange.ClearContents
ReDim w(1 To UBound(a, 2))
With CreateObject("Scripting.Dictionary")
For i = 1 To UBound(a, 1)
If a(i, 1) <> "" Then
If Not .exists(a(i, 1)) Then
For ii = 1 To UBound(a, 2)
w(ii) = a(i, ii)
Next
.Item(a(i, 1)) = w
End If
End If
Next
a = Sheets("DataSheet").Cells(1).CurrentRegion.Value
For i = 2 To UBound(a, 1)
If a(i, 1) <> "" Then
If .exists(a(i, 1)) Then
w = .Item(a(i, 1))
Else
ReDim w(1 To UBound(w))
End If
For ii = 1 To UBound(a, 2)
w(ii) = a(i, ii)
Next
.Item(a(i, 1)) = w
End If
Next
a = Application.Index(.items, 0, 0)
End With
.DataBodyRange.Resize(UBound(a, 1)).Value = a
End With
End Sub
Bookmarks