Delete rows based on a specific Column

jxj_00

New Member
Joined
Oct 1, 2020
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hi

I have a database that needs to be regularly updated and to prevent any duplication of records the codes need to do the following:
1. To delete the entire row if any duplicates in Column A (Employee ID)
2. To duplicate the first entry (Meaning if there was a duplicate of row 1 with row 15 it will delete row 1)

I have attach a copy of the excel in this link: Box

VBA Code:
Sub RemoveDuplicateRows()

Dim MyRange As Range
Dim LastRow As Long

LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Set MyRange = ActiveSheet.Range("A1:D" & LastRow)
MyRange.RemoveDuplicates Columns:=1, Header:=xlYes

End Sub
 

Trixterz

Board Regular
Joined
Aug 15, 2019
Messages
59
Your sheet is probably locked or protected. Try manually deleting that row 1004 and see if you get prompted...
 

Some videos you may like

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,368
Office Version
  1. 365
Platform
  1. Windows
Once again check that you don't have any merged cells & that the sheet is not protected.
 

jxj_00

New Member
Joined
Oct 1, 2020
Messages
23
Office Version
  1. 365
Platform
  1. Windows
I see, let me take note of it when I run into an error again. Appreciate your help and patience!
 

jxj_00

New Member
Joined
Oct 1, 2020
Messages
23
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

I inserted a code to unmerge all cells & turn off the display alert before deleting the duplicates. The code is running smoothly without any errors. Appreciate the advice given.

VBA Code:
Sub jxj()
   Dim i As Long
   Dim Rng As Range
   Dim desWS As Worksheet
   Set desWS = Workbooks("WB1.xlsm").Sheets("WS1")
   Application.ScreenUpdating = False
   Application.DisplayAlerts = False 
   desWS.Cells.UnMerge
    Application.DisplayAlerts = True
   
   desWS.Activate
   With CreateObject("scripting.dictionary")
      For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
         If Not .Exists(Cells(i, 1).Value) Then
            .Add Cells(i, 1).Value, Nothing
         Else
            If Rng Is Nothing Then Set Rng = Cells(i, 1) Else Set Rng = Union(Rng, Cells(i, 1))
         End If
      Next i
   End With
   If Not Rng Is Nothing Then Rng.EntireRow.Delete
   Application.ScreenUpdating = True
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,368
Office Version
  1. 365
Platform
  1. Windows
Glad you sorted it & thanks for the feedback.
 

jxj_00

New Member
Joined
Oct 1, 2020
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Glad you sorted it & thanks for the feedback.
Hi, sorry to trouble you again. Would you mind explaining the code as I am trying to repurpose this code for something else? Thank you in advance.
 

jxj_00

New Member
Joined
Oct 1, 2020
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hi, sorry to trouble you again. Would you mind explaining the code as I am trying to repurpose this code for something else? Thank you in advance.
I had a bit of confusion with the union part of the code but I got it sorted. Appreciate the help given.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,251
Messages
5,623,612
Members
415,981
Latest member
Baltwin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top