+ Reply to Thread
Results 1 to 15 of 15

Deleting double-consecutively repeated columns and leaving 4-consecutively repeated column

Hybrid View

  1. #1
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Deleting double-consecutively repeated columns and leaving 4-consecutively repeated co

    Hi, Deep,

    avoid problems with the counter when deleting by working from the bottom up (otherwise you may miss a couple of rows in the check).

    Sub DeleteDup()
    
    Dim lngCounter As Long
    
    For lngCounter = Cells(Rows.Count, 1).End(xlUp).Row - 1 To 1 Step -1
        
      If Application.WorksheetFunction.CountIf(Range("A:A"), Cells(lngCounter, 1).Value) = 2 Then
        Range(Cells(lngCounter - 1, 1), Cells(lngCounter, 1)).EntireRow.Delete
      End If
    
    Next lngCounter
                
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  2. #2
    Registered User
    Join Date
    05-10-2013
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: Deleting double-consecutively repeated columns and leaving 4-consecutively repeated co

    Hello everyone, I've tried the following code,

    Dim longCounter As Long

    With Worksheets("MAY 8")
    For longCounter = Cells(rows.Count, 1).End(xlUp).Row - 1 To 1 Step -1
    If Application.WorksheetFunction.CountIf(Range("Q:Q"), Cells(longCounter, 1).Value) = 2 Then
    Range(Cells(longCounter - 1, 1), Cells(longCounter, 1)).EntireRow.Delete
    End If
    Next longCounter
    End With


    It doesn't seem to delete the 2 consecutive repeated ones, any reasons?

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Deleting double-consecutively repeated columns and leaving 4-consecutively repeated co

    Hi, exlgh91,

    because you didnīt use code-tags when posting here? (just kidding)

    The code will be executed on the active sheet although you want it to execute on a non-active sheet:
    Dim longCounter As Long
    
    With Worksheets("MAY 8")
      For longCounter = .Cells(Rows.Count, 1).End(xlUp).Row - 1 To 1 Step -1
        If Application.WorksheetFunction.CountIf(.Range("Q:Q"), .Cells(longCounter, 1).Value) = 2 Then
          .Range(.Cells(longCounter - 1, 1), .Cells(longCounter, 1)).EntireRow.Delete
        End If
      Next longCounter
    End With
    Ciao,
    Holger

  4. #4
    Registered User
    Join Date
    05-10-2013
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: Deleting double-consecutively repeated columns and leaving 4-consecutively repeated co

    Hi Holger, sorry to bother you again but still no luck.
    I have attached a sample file, where I want to use column B data for consecutive repetitiveness.
    Much thanks in advance!Test.xls

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Deleting double-consecutively repeated columns and leaving 4-consecutively repeated co

    Hi, exlgh91,

    Cells wants the Row first and the Column thereafter either being noted as a string or as the Column number. A is 1 so you were checking the wrong column for the dupes. And I should have noticed that checking Column Q with a value from Column A doesnīt make that much sense.

    This code will check for Column B, if you want it to work on any other column just change the value of the Constant cstrCOL.
    Sub Delete2()
    
    Dim lngCounter As Long
    
    Const cstrCOL As String = "B"
    
    For lngCounter = Cells(Rows.Count, cstrCOL).End(xlUp).Row - 1 To 1 Step -1
        If Application.WorksheetFunction.CountIf(Range(Cells(1, cstrCOL), Cells(Rows.Count, cstrCOL)), Cells(lngCounter, cstrCOL).Value) = 2 Then
            Range(Cells(lngCounter - 1, cstrCOL), Cells(lngCounter, cstrCOL)).EntireRow.Delete
        End If
    Next lngCounter
    End Sub
    Ciao,
    Holger

  6. #6
    Registered User
    Join Date
    05-10-2013
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: Deleting double-consecutively repeated columns and leaving 4-consecutively repeated co

    This is great much thanks! Just a quick question though, it works by deleting everything other than 4 repetitives, but still leaves 2 irrelevant ones at the bottom (does this have something to do with -1 TO 1 Step -1? and what does it mean?). Wish I could buy you a pitcher of beer or something..

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Deleting double-consecutively repeated columns and leaving 4-consecutively repeated co

    Hi, exlgh91,

    the code was exclusively looking for doubled lines by using WorksheetFunction.CountIf(..) = 2. If you want it changed the code must be altered as the lines to delete could interfere with other existing lines which should not be deleted.

    Maybe you go have a go with
    Sub Delete3()
    
    Dim lngCounter As Long
    
    Const cstrCOL As String = "B"
    
    For lngCounter = Cells(Rows.Count, cstrCOL).End(xlUp).Row To 2 Step -1
        Select Case WorksheetFunction.CountIf(Range(Cells(1, cstrCOL), Cells(Rows.Count, cstrCOL)), Cells(lngCounter, cstrCOL).Value)
          Case 1
            Cells(lngCounter, cstrCOL).EntireRow.Delete
          Case 2
            Range(Cells(lngCounter - 1, cstrCOL), Cells(lngCounter, cstrCOL)).EntireRow.Delete
          Case 3
            Range(Cells(lngCounter - 2, cstrCOL), Cells(lngCounter, cstrCOL)).EntireRow.Delete
          Case Else
            'do nothing
        End Select
    Next lngCounter
    End Sub
    Ciao,
    Holger

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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