Consulting

Results 1 to 10 of 10

Thread: Remove duplicates from Rows with the last Row only as reference

  1. #1
    Banned VBAX Regular
    Joined
    Feb 2017
    Posts
    14
    Location

    Remove duplicates from Rows with the last Row only as reference

    Hello, I would like to apologize first for my bad english

    I need help editing a VBA macro.

    The macro deletes all duplicates per cell (except the first one) on all columns,raws and starts with the first row.

    I would like to change the macro to take the last row only as reference

    this is the macro

    Sub DeleteDuplicateEntries() 
    Dim Cell As Range, Cel As Range, N& 
    Application.ScreenUpdating = False 
    N = 0 
    For Each Cell In Selection 
    '1st loop - (to speed things up ignore any empty cells)
    If Cell <> Empty Then 
    For Each Cel In Selection 
    '2nd loop - compare non-empty cel values
    'and clear contents if it's a duplicated value
    If Cel <> Empty And _ 
    Cel.Value = Cell.Value And _ 
    Cel.Address <> Cell.Address Then 
    Cel.ClearContents 
    N = N + 1 
    End If 
    Next Cel 
    End If 
    Next 
    Application.ScreenUpdating = True 
    MsgBox "There were " & N & " duplicated entries deleted" 
    End Sub
    for example
    A B C D E F G
    10 17 25 29 38 44 45
    6 9 28 34 35 41 47
    9 12 19 26 32 33 38
    9 16 26 29 34 43 44
    become

    A B C D E F G
    10 17 25 38 45
    6 28 35 41 47
    12 19 32 33 38
    9 16 26 29 34 43 44

    thnx for your help and time
    Last edited by SamT; 02-16-2017 at 02:15 PM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Change
    For Each Cell In Selection
    To
    For Each Cell In Selection.Rows(Selection.Rows.Count).Row
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Banned VBAX Regular
    Joined
    Feb 2017
    Posts
    14
    Location
    thx for your reply and time

    but i got error run-time error '424' object required

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    try changing one character in your code:
    Cell.ClearContents
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    My bad. Too many "Rows" above
    With Selection
    For each Cell In .Rows(.Rows.Count)
    If Not IsEmpty(Cell) Then
    For each cel In .Cells(1).Resize(.Rows.Count - 1, .Columns.Count)
    If Cel = Cell Then Cel.ClearContents
    Next
    End If
    next
    End With
    Faster
    Dim ChkRng As Range,TestRng As Range, Cell As Range, Cel As Range 
    Dim n as long
    
    With Selection
    Set ChkRng = .Rows(.Rows.Count)
    Set TestRng = .Cells(1).Resize(.Rows.Count - 1, .Columns.Count)
    End With
    
    For each Cell in ChkRng
    If Not IsEmpty(Cell) Then
    For Each Cel in TestRng
    If Cel = Cell Then Cel.ClearContents
    Next
    End If
    next
    
    MsgBox n
    If you need more speed, use arrays
    Last edited by SamT; 02-16-2017 at 09:16 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    SamT interpreted your requirement better than I did!
    Quote Originally Posted by SamT View Post
    If you need more speed, use arrays
    Sub DeleteDuplicateEntries2()
    Dim ChkRng, TestRng As Range, TRVals, rv, rw As Long, colm As Long, n As Long
    
    With Selection
      ChkRng = .Rows(.Rows.Count)
      Set TestRng = .Cells(1).Resize(.Rows.Count - 1, .Columns.Count)
      TRVals = TestRng.Value
    End With
    
    For Each rv In ChkRng
      If Not IsEmpty(rv) Then
        For rw = 1 To UBound(TRVals)
          For colm = 1 To UBound(TRVals, 2)
            If TRVals(rw, colm) = rv Then
              TRVals(rw, colm) = Empty
              n = n + 1
            End If
          Next
        Next
      End If
    Next
    TestRng = TRVals
    MsgBox n
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    Banned VBAX Regular
    Joined
    Feb 2017
    Posts
    14
    Location
    thx for your time and solution

    work great.

  8. #8
    Banned VBAX Regular
    Joined
    Feb 2017
    Posts
    14
    Location
    hi,
    is posible to add a color to the deleted cells after

    or Each rv In ChkRng
    If Not IsEmpty(rv) Then
    For rw = 1 To UBound(TRVals)
    For colm = 1 To UBound(TRVals, 2)
    If TRVals(rw, colm) = rv Then
    TRVals(rw, colm) = Empty

    i did try with cells(rw, colm).interior.color = rgb (255,0,0)

    but doesn't work as i excpected

    thx for your help

  9. #9
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Sub DeleteDuplicateEntries3()
    Dim ChkRng, TestRng As Range, TRVals, rv, rw As Long, colm As Long, n As Long, RedRng As Range
    
    With Selection
      ChkRng = .Rows(.Rows.Count)
      Set TestRng = .Cells(1).Resize(.Rows.Count - 1, .Columns.Count)
      TRVals = TestRng.Value
    End With
    
    For Each rv In ChkRng
      If Not IsEmpty(rv) Then
        For rw = 1 To UBound(TRVals)
          For colm = 1 To UBound(TRVals, 2)
            If TRVals(rw, colm) = rv Then
              TRVals(rw, colm) = Empty
              If RedRng Is Nothing Then Set RedRng = TestRng.Cells(rw, colm) Else Set RedRng = Union(RedRng, TestRng.Cells(rw, colm))
              n = n + 1
            End If
          Next
        Next
      End If
    Next
    
    If n > 0 Then
      TestRng = TRVals
      RedRng.Interior.Color = RGB(255, 0, 0)
    End If
    MsgBox n
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  10. #10
    Banned VBAX Regular
    Joined
    Feb 2017
    Posts
    14
    Location
    work perefect

    thx for your time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •