+ Reply to Thread
Results 1 to 9 of 9

How to control of repeating procedures? (delete duplicates rows)

Hybrid View

Luu4466 How to control of repeating... 07-18-2021, 02:13 PM
TMS Re: How to control of... 07-18-2021, 03:24 PM
jolivanes Re: How to control of... 07-18-2021, 03:46 PM
Luu4466 Re: How to control of... 07-18-2021, 04:36 PM
TMS Re: How to control of... 07-18-2021, 04:38 PM
Luu4466 Re: How to control of... 07-18-2021, 04:50 PM
TMS Re: How to control of... 07-18-2021, 05:36 PM
Luu4466 Re: How to control of... 07-19-2021, 09:56 AM
TMS Re: How to control of... 07-19-2021, 11:02 AM
  1. #1
    Forum Contributor
    Join Date
    11-07-2020
    Location
    Paris
    MS-Off Ver
    Microsoft office 365
    Posts
    236

    How to control of repeating procedures? (delete duplicates rows)

    I would like to create a structure for repeated operations by using Do While Loop. The DElDuplicates, which searches for and deletes duplicates rows in a database is a good example to explain the situation. Having sorted the database on a specific 'code' field, the entries in the code column are processed row-by row. If the values of the currentCell and nextCell are the same, the currentCell row is deleted.

    I think my problem is caused by the reference objects. I don't know how to name it in my worksheet.

    Sub DelDuplicates()
    Dim currentCell, nextCell
    Range("database").Sort key1:=Range("code") ----------- error here?
    Set currentCell = Range("code")
    Do While Not IsEmpty(currentCell)
      Set nextCell = currentCell.Offset(1, 0)
      If nextCell.Value = currentCell.Value Then
        currentCell.EntireRow.Delete
      End If
      Set currentCell = nextCell
    Loop
    End Sub
    Capture.PNG
    Attached Files Attached Files
    Last edited by Luu4466; 07-19-2021 at 09:57 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,121

    Re: How to control of repeating procedures? (delete duplicates rows)

    Not sure if this helps

    Option Explicit
    
    Sub DelDuplicates()
    
    ' Named Ranges:
    '   Fixed/static range C1
    '       Code: =INDEX('repeating procedure'!$C:$C,1)
    '   Dynamic Named Range
    '       database: ='repeating procedure'!$A$1:INDEX('repeating procedure'!$C:$C, COUNT('repeating procedure'!$A:$A))
    
    Dim currentCell As Range, nextCell As Range
    
    ' sort Range("database") using Range("Code")
    ' effectively sorting on column C
    Range("database").Sort key1:=Range("code")
    
    Set currentCell = Range("code") ' start with cell C1
    
    Do While Not IsEmpty(currentCell)
        Set nextCell = currentCell.Offset(1, 0)
            If nextCell.Value = currentCell.Value Then
                currentCell.EntireRow.Delete
            End If
        Set currentCell = nextCell
    Loop
    
    End Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: How to control of repeating procedures? (delete duplicates rows)

    Don't know if I understood your requirement right.
    When deleting I prefer to start at the bottom, even when it is not required at the odd instance.
    Sub Maybe()
    Dim i As Long
    Application.ScreenUpdating = True
        For i = Cells(Rows.Count, 3).End(xlUp).Row To 2 Step -1    '<---- the 3 is for the 3rd column, Column C
            If Cells(i, 3).Value = Cells(i, 3).Offset(-1).Value Then Cells(i, 3).Offset(-1).EntireRow.Delete
        Next i
    Application.ScreenUpdating = True
    End Sub

  4. #4
    Forum Contributor
    Join Date
    11-07-2020
    Location
    Paris
    MS-Off Ver
    Microsoft office 365
    Posts
    236

    Re: How to control of repeating procedures? (delete duplicates rows)

    Thank you TMS for your answer. It doesn't work because I have an error signal.
    Capture.PNG

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,121

    Re: How to control of repeating procedures? (delete duplicates rows)

    You need to create Named Ranges as described at the beginning of the routine otherwise the code does not know what to sort, or on what basis.

  6. #6
    Forum Contributor
    Join Date
    11-07-2020
    Location
    Paris
    MS-Off Ver
    Microsoft office 365
    Posts
    236

    Re: How to control of repeating procedures? (delete duplicates rows)

    I don't know how to name the range and where to name the range.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,121

    Re: How to control of repeating procedures? (delete duplicates rows)

    See the updated example.
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    11-07-2020
    Location
    Paris
    MS-Off Ver
    Microsoft office 365
    Posts
    236

    Re: How to control of repeating procedures? (delete duplicates rows)

    Thank you very much it's working, and screen capture were useful to understand.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,121

    Re: How to control of repeating procedures? (delete duplicates rows)

    You're welcome. Thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Delete Repeating Groups of Rows
    By jn83666 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-31-2016, 03:07 PM
  2. [SOLVED] VBA Code to have a repeating form control /Active X control (List box) in every row
    By Ehezve in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-28-2013, 08:55 PM
  3. How to Delete Repeating Values in Many Rows?
    By sadrap in forum Excel General
    Replies: 1
    Last Post: 01-06-2013, 12:49 PM
  4. Replies: 1
    Last Post: 11-12-2012, 03:03 PM
  5. Delete repeating heading rows
    By xyz123 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-21-2008, 06:32 AM
  6. [SOLVED] delete repeating rows
    By aledger in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-04-2005, 05:06 PM
  7. [SOLVED] Closing form turns off commandbar control procedures??
    By Caro-Kann Defence in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-28-2005, 06:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1