Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7429179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:55:40+00:00 2026-05-29T08:55:40+00:00

Trying to run a macro in Excel to remove non dupes so dupes can

  • 0

Trying to run a macro in Excel to remove non dupes so dupes can be examined easily.

Step through each cell in column “B”, starting at B2 (B1 is header)

During run, if current cell B has a match anywhere in column B – leave it, if it’ unique – remove entire row

The code below is executing with inconsistent results.

Looking for some insight

Sub RemoveNonDupes()
 Selection.Copy
 Range("B2").Select
 ActiveSheet.Paste
 Application.CutCopyMode = False
 Range("B2:B5000").AdvancedFilter Action:= xlFilterInPlace,  CriteriaRange:= Range("B2"), Unique := True
 Range("B2:B5000").SpecialCells(xlCellTypeVisible).EntireRow.Delete
 ActiveSheet.showalldata
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:55:41+00:00Added an answer on May 29, 2026 at 8:55 am

    Not the most direct route, but you could have the macro insert between B and C. Then dump a formula in that column that counts.

    Something like =countifs(B:B,B:B) That will give you a count of how many times a record shows, then you can set the script to Loop deleting any row where that value is 1.

    Something like

    Sub Duplicates()
    
    Columns("B:B").Insert Shift:=xlToRight ' inserts a column after b
    
    count = Sheet1.Range("B:B").Cells.SpecialCells(xlCellTypeConstants).count ' counts how many records you have
    
    crange = "C1:C" & count ' this defines the range your formula's go in if your data doesn't start in b1, change the c1 above to match the row your data starts
    
    Sheet1.Range(crange).Formula = "=countifs(B:B,B:B)"  ' This applies the same forumla to the range
    
    ct=0
    ct2=0  'This section will go cell by cell and delete the entire row if the count value is 1
    Do While ct2 < Sheet1.Range("C:C").Cells.SpecialCells(xlCellTypeConstants).count
        For ct = 0 To Sheet1.Range("C:C").Cells.SpecialCells(xlCellTypeConstants).count
            If Sheet1.Range("C1").Offset(ct, 0).Value > 1 Then
                Sheet1.Range("C1").Offset(ct, 0).EntireRow.Delete
            End If
    
        Next
    ct2 = ct2 + 1
    
    Loop
    Sheet1.Columns("B:B").EntireColumn.delete
    end sub
    

    Code isn’t pretty, but it should do the job.

    **Updated code per comments

    Sub Duplicates()
    
    Columns("C:C").Insert Shift:=xlToRight ' inserts a column after b
    
    count = Activesheet.Range("C:C").Cells.SpecialCells(xlCellTypeConstants).count ' counts how many records you have
    
    crange = "C1:C" & count ' this defines the range your formula's go in if your data doesn't start in b1, change the c1 above to match the row your data starts
    
    Activesheet.Range(crange).Formula = "=countifs(B:B,B:B)"  ' This applies the same forumla to the range
    
    
    ct=0
    ct2=0  'This section will go cell by cell and delete the entire row if the count value is 1
    '''''
    Do While ct2 < Activesheet.Range("C:C").Cells.SpecialCells(xlCellTypeConstants).count
        For ct = 0 To Activesheet.Range("C:C").Cells.SpecialCells(xlCellTypeConstants).count
            If Activesheet.Range("C1").Offset(ct, 0).Value = 1 Then
                Activesheet.Range("C1").Offset(ct, 0).EntireRow.Delete
            End If
    
        Next
    ct2 = ct2 + 1
    
    Loop
    ActiveSheet.Columns("C:C").EntireColumn.delete  
    end sub
    

    You can try that updated code, the part with the Do Loop is what will delete each column, I fixed it to delete any row where the count is 1.
    Based on what I understand, your data should be in column B and the counts should be in column C. If that isn’t correct, update the formula’s to match

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run the below-mentioned code in VB(Excel Macro) but I'm stuck with
I'm trying to run an Excel macro from outside of the Excel file. I'm
I am trying to trigger a macro to run when an Excel document is
I'm trying to set up solver for each run of a macro but it
I am trying to build a macro that cycles through a column of cells
I am trying to run a certain macro in very short intervals, such as
I am writing a macro in Excel and I am trying to save a
I'm trying to write an Excel macro using VBA to automate importing CSV text
I'm trying to call an Excel macro that's in another workbook. It's a sheet-specific
I am trying to use regular expressions in a macro for an Excel document.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.