Consulting

Results 1 to 12 of 12

Thread: Solved: Delete duplicate data in a Column

  1. #1
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location

    Solved: Delete duplicate data in a Column

    I Have a Problem with this VBA code , i want to delete just duplicate data on Column B , just Column B . but this code delete row also .
    that beter i write i want to remove duplicate just i 1 Column and also remove the blank cell on that column alo .

    Please help me .


    PHP Code:
    Dim x               As Long
    Dim LastRow         
    As Long
        LastRow 
    Range("B65536").End(xlUp).Row
        
    For LastRow To 1 Step -1
            
    If Application.WorksheetFunction.CountIf(Range("B1:B" x), Range("B" x).Text) > 1 Then
                Range
    ("B" x).EntireRow.Delete
            End 
    If
        
    Next x 

  2. #2
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    This part deletes entire row:
    [VBA]Range("B" & x).EntireRow.Delete[/VBA]
    so change it to:
    [VBA]Range("B" & x).Delete[/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  3. #3
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Thank you for your help , it is ok but there is 1 problem and 1 question .

    I have A - B - C - D column when use your code all duplicate data on column B romoved but the latest data of column C add to end of column B . where is the probme?

    if i want to use this code for A- B - C - D column that mean remove duplicate data of each column , please help me how can do it .

  4. #4
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    This should settle the first part:
    [VBA]Range("B" & x").Delete Shift:=xlUp[/VBA]

    I am little unclear about your requirement:

    Do you mean check for duplicates in each column separately and deleting them?
    e.g. Duplicate found in B2 so delete B2 only.

    Or When duplicate is found in column B then delete four cells together?
    e.g. Duplicate found in B2 so delete A22?
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  5. #5
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Yes , I want check for duplicates in each column separately and deleting them.

    Thank you very much for your big help .

  6. #6
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Then this should work:
    [VBA]Dim i As Integer
    Dim x As Long
    Dim LastRow As Long
    Application.ScreenUpdating = False
    For i = 1 To 4
    LastRow = Cells(65536, i).End(xlUp).Row
    For x = LastRow To 1 Step -1
    If Application.CountIf(Range(Cells(1, i), Cells(x, i)), Cells(x, i).Value) > 1 Then
    Cells(x, i).Delete Shift:=xlUp
    End If
    Next x
    Next i
    Application.ScreenUpdating = True[/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  7. #7
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    You are the best mam . you help me so much and thank you for your help .

  8. #8
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    just my latest question , could please add a code to remove blank celll also ?

  9. #9
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    OK. I am assuming that there are no cells with formulas that give blanks.
    [vba]Dim i As Integer
    Dim x As Long
    Dim LastRow As Long
    For i = 1 To 4
    LastRow = Cells(65536, i).End(xlUp).Row
    For x = LastRow To 1 Step -1
    If Application.CountIf(Range(Cells(1, i), Cells(x, i)), Cells(x, i).Value) > 1 Then
    Cells(x, i).Delete Shift:=xlUp
    End If
    Next x
    On Error Resume Next
    Range(Cells(1, i), Cells(LastRow, i)).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
    On Error GoTo 0
    Next i[/vba]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  10. #10
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Thank you So much For your Helo Man .
    Have a Good Day .

  11. #11
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    Fr this one please add the code to remove blank cell after delete,

    Pleaseeeeeeeeee

    PHP Code:
    Dim x               As Long
    Dim LastRow         
    As Long
        LastRow 
    Range("B65536").End(xlUp).Row
        
    For LastRow To 1 Step -1
            
    If Application.WorksheetFunction.CountIf(Range("B1:B" x), Range("B" x).Text) > 1 Then
                Range
    ("B" x).Delete Shift:=xlUp
            End 
    If
        
    Next x 

  12. #12
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    it is done .

    Thank you very much shrivallabha


    PHP Code:
     

    Dim x               
    As Long
    Dim LastRow         
    As Long
        LastRow 
    Range("B65536").End(xlUp).Row
        
    For LastRow To 1 Step -1
            
    If Application.WorksheetFunction.CountIf(Range("B1:B" x), Range("B" x).Text) > 1 Then
                Range
    ("B" x).Delete Shift:=xlUp
            End 
    If
        
    Next x  
        On Error Resume Next 
        Range
    (Cells(1"B"), Cells(LastRow"B")).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp 
        On Error 
    Goto 
    Next B 

Posting Permissions

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