Remove duplicate but move a value to another cell firsts.

livetolearn4life

New Member
Joined
Jul 14, 2020
Messages
15
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
Hello everyone

so what im trying to do , is if Cell A:A has duplicate value then take value in J and L and move them up on the same row as the first instance in column A then remove the duplicates rows.

im am close with this code it's just that but i can't figure the last part of out.



VBA Code:
Sub SPIN()

   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long, c As Long

  Sheets("RAW DATA").Select
   Ary = Sheets("RAW DATA").Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary), 1 To 12)

   For r = 2 To UBound(Ary)
      If Ary(r, 1) <> Ary(r - 1, 1) Then
         nr = nr + 1
         For c = 1 To 12
            Nary(nr, c) = Ary(r, c)
         Next c
      Else
         Nary(nr, 12) = Nary(nr, 12) & ", " & Ary(r, 12)
      End If
   Next r
   Sheets("WHAT I NEED").Select
   Sheets("WHAT I NEED").Range("A2").Resize(nr, 12).Value = Nary
  
End Sub


1636501555330.png



RECSEQNUMNAMECODEDATE ISSLOCATIONPLANORDERCLIENTSHOPOPER#DATE OPERATION COMPLETE
1609776661658884525blab067gHRiplby-bNyERgEALiplBTIaa1/4/2021glused8260
GMT
51/6/2021INS1/19/2021GRT1/20/2021
1609776661658884525blab067gHRiplby-bNyERgEALiplBTIaa1/4/2021glused8311INS101/19/2021
1609776661658884525blab067gHRiplby-bNyERgEALiplBTIaa1/4/2021glused8220GRT151/20/2021THEN DELETE DUPLICATE ROWS
1609776705738884525blab068gHRiplby-bNyERgEALiplBTIaa1/4/2021glused8260GMT51/6/2021
1609776705738884525blab068gHRiplby-bNyERgEALiplBTIaa1/4/2021glused8311INS101/19/2021
1609776705738884525blab068gHRiplby-bNyERgEALiplBTIaa1/4/2021glused8220GRT151/20/2021
1609784640026885948blab017PALiplBNEL-IALiplBTCTRg1aa1/4/2021glused7RG260GMT51/4/2021
1609784640026885948blab017PALiplBNEL-IALiplBTCTRg1aa1/4/2021glused7RG311INS101/27/2021
1609784640026885948blab017PALiplBNEL-IALiplBTCTRg1aa1/4/2021glused7RG220GRT151/27/2021
160978817770541611050blab11TbBE REALiplBRaa1/4/2021glused7NG260GMT51/21/2021
160978817770541611050blab11TbBE REALiplBRaa1/4/2021glused7NG311INS101/28/2021
160978817770541611050blab11TbBE REALiplBRaa1/4/2021glused7NG220GRT151/28/2021
160986231854241451064blab3ALiplBRM HiplbgING iplbTERaa1/5/2021glused1NG220GMT51/5/2021
160986231854241451064blab3ALiplBRM HiplbgING iplbTERaa1/5/2021glused1NG221GMT101/12/2021
160986231854241451064blab3ALiplBRM HiplbgING iplbTERaa1/5/2021glused1NG220GRT151/12/2021
1609882057160885422blab411LEG ALiplBggY-ALiplBIgLEaa1/5/2021glused1NG221GMT51/14/2021
1609882057160885422blab411LEG ALiplBggY-ALiplBIgLEaa1/5/2021glused1NG311INS109/15/2021
1609882057160885422blab411LEG ALiplBggY-ALiplBIgLEaa1/5/2021glused1NG221GRT159/15/2021
1609882095985885422blab411LEG ALiplBggY-ALiplBIgLEaa1/5/2021glused1NG221GMT51/20/2021
1609882095985885422blab411LEG ALiplBggY-ALiplBIgLEaa1/5/2021glused1NG311INS109/15/2021
 
Last edited by a moderator:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
When posting vba code, please use the available code tags. My signature block below has more details. I fixed it for you this time.

I'm wondering how big your data is? It may be that an array approach is used, but you could see if this non-array approach does what you want sufficiently well.

VBA Code:
Sub Test()
  Dim r As Long, tr As Long
  
  Application.ScreenUpdating = False
  Sheets("RAW DATA").Copy After:=Sheets("RAW DATA")
  With ActiveSheet
    For r = 2 To .Range("A" & Rows.Count).End(xlUp).Row
      If .Range("A" & r).Value <> .Range("A" & r - 1).Value Then
        tr = r
      Else
        .Cells(tr, Columns.Count).End(xlToLeft).Offset(, 1).Resize(, 2).Value = Array(.Cells(r, 10).Value, .Cells(r, 12).Value)
      End If
    Next r
    .UsedRange.RemoveDuplicates Columns:=1
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
When posting vba code, please use the available code tags. My signature block below has more details. I fixed it for you this time.

I'm wondering how big your data is? It may be that an array approach is used, but you could see if this non-array approach does what you want sufficiently well.

VBA Code:
Sub Test()
  Dim r As Long, tr As Long
 
  Application.ScreenUpdating = False
  Sheets("RAW DATA").Copy After:=Sheets("RAW DATA")
  With ActiveSheet
    For r = 2 To .Range("A" & Rows.Count).End(xlUp).Row
      If .Range("A" & r).Value <> .Range("A" & r - 1).Value Then
        tr = r
      Else
        .Cells(tr, Columns.Count).End(xlToLeft).Offset(, 1).Resize(, 2).Value = Array(.Cells(r, 10).Value, .Cells(r, 12).Value)
      End If
    Next r
    .UsedRange.RemoveDuplicates Columns:=1
  End With
  Application.ScreenUpdating = True
End Sub


Hi Peter,
thank you for the response and for the insight on the code tags.

i tried non-array approach you provided and the code was still running after 15 minutes so i finally killed excel through the task manager. i think the array approach would be the best for this case.

in regards to my data size, it is currently at 50,000 rows and 12 columns but could easily grow to double and triple that size depending on the filter i apply coming from the data base.



the code extends keeps going out additional columns to the point that it runs out of columns as shown next.

1636554301092.png


and this next image is what im trying to accomplish.
1636555024855.png
 
Upvote 0
Give this one a try. I have assumed sheet 'WHAT I NEED' exists but can have any contents/formatting removed.

VBA Code:
Sub Test_v2()
  Dim a As Variant, b As Variant
  Dim r As Long, c As Long, nr As Long, nc As Long, uba2 As Long, maxcols As Long
  
  a = Sheets("RAW DATA").ListObjects(1).Range.Value
  uba2 = UBound(a, 2)
  maxcols = uba2
  ReDim b(1 To UBound(a), 1 To uba2)
  For r = 2 To UBound(a)
    If a(r, 1) = a(r - 1, 1) Then
      If nc > maxcols Then
        ReDim Preserve b(1 To UBound(b), 1 To nc + 1)
        maxcols = nc + 1
      End If
      b(nr, nc) = a(r, 10)
      b(nr, nc + 1) = a(r, 12)
      nc = nc + 2
    Else
      nr = nr + 1
      For c = 1 To uba2
        b(nr, c) = a(r, c)
      Next c
      nc = uba2 + 1
    End If
  Next r
  With Sheets("WHAT I NEED")
    .UsedRange.Clear
    With .Range("A2").Resize(nr, maxcols)
      .Value = b
      .Rows(0).Resize(, uba2).Value = a
      .Parent.ListObjects.Add xlSrcRange, .CurrentRegion, , xlYes
      .EntireColumn.AutoFit
    End With
  End With
End Sub
 
Upvote 0
Solution
Give this one a try. I have assumed sheet 'WHAT I NEED' exists but can have any contents/formatting removed.

VBA Code:
Sub Test_v2()
  Dim a As Variant, b As Variant
  Dim r As Long, c As Long, nr As Long, nc As Long, uba2 As Long, maxcols As Long
 
  a = Sheets("RAW DATA").ListObjects(1).Range.Value
  uba2 = UBound(a, 2)
  maxcols = uba2
  ReDim b(1 To UBound(a), 1 To uba2)
  For r = 2 To UBound(a)
    If a(r, 1) = a(r - 1, 1) Then
      If nc > maxcols Then
        ReDim Preserve b(1 To UBound(b), 1 To nc + 1)
        maxcols = nc + 1
      End If
      b(nr, nc) = a(r, 10)
      b(nr, nc + 1) = a(r, 12)
      nc = nc + 2
    Else
      nr = nr + 1
      For c = 1 To uba2
        b(nr, c) = a(r, c)
      Next c
      nc = uba2 + 1
    End If
  Next r
  With Sheets("WHAT I NEED")
    .UsedRange.Clear
    With .Range("A2").Resize(nr, maxcols)
      .Value = b
      .Rows(0).Resize(, uba2).Value = a
      .Parent.ListObjects.Add xlSrcRange, .CurrentRegion, , xlYes
      .EntireColumn.AutoFit
    End With
  End With
End Sub
You Nailed it Peter! I appreciate your assistance with this very much sir! Hope you have a blessed and great day!
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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