Remove Duplicates Rows completely from sheet

Hey How are You friends ? I know you are fine but I am in a big Problem and I know you will help me after solving my problem. Today I got a task  for removing duplicate rows from sheet, and my guide provided me two scenarios. First scenarios is not so hard and it is easily done by me but second scenarios is not we could not say it is so hard although I have solve this Problem also but  the way from which I have solve this problem is not satisfactory for me hence please give me some sun shine to come out from this darkness. I am discussing both scenarios here and please provide suitable answer for doing this task.
Note : Value of all column approximately remains same but
First Scenario : Contains Parameter that decide Duplicates

In this case first column of sheet contains duplicate values as shown in figure below so, it easy to remove all those rows by using RemoveDuplicates function 

In this figure Date is parameter by which we decide row is repeated or not, if it is repeated then we have to delete that row as I am doing by using this code.

Sub firstScanarios()

Dim last_Row As Integer
Sheets("sheet1").Activate
                          
'first count number of rows

last_Row = Range("A" & Rows.Count).End(xlUp).Row

'RemoveDuplicates fnction checks duplicate values inside sheet and remove automatically from sheet

Range("A2" & ":" & "D" & last_Row).RemoveDuplicates Columns:=Array(1), Header:=xlNo

MsgBox "Done"

End Sub
After Adding this code inside Developer mode

I use command Alt+F8 and obtain this result


In this case I don’t obtain any problem and my target is completed successfully.
Second Scenario :
In this case sheet is like this 


And I have to remove duplicates rows from sheet for this purpose I have perform same action as I previously done but because my duplicate deciding parameter column 2 so I delete all those parameters which after second column but can’t remove those columns which before second column so, to remove these columns I used another logic that I checked all those rows which are blank after performing first logic  I got such result :

I used Same code as I had done previously only I have changed deciding parameter column

Sub secondScanarios()

Dim last_Row As Integer

Sheets("sheet2").Activate

'first count number of rows

last_Row = Range("A" & Rows.Count).End(xlUp).Row

'RemoveDuplicates fnction checks duplicate values inside sheet and remove automatically from sheet

Range("B2" & ":" & "D" & last_Row).RemoveDuplicates Columns:=Array(1), Header:=xlNo
MsgBox "Done"End Sub

To remove all those rows completely which contains duplicates I used another logic

For i = 1 To last_Row

‘Column 2  contains deciding parameter so checks after completing first logic which cell contains null value

If Cells(i, 2).Value = "" Then        

Rows(i & ":" & Rows.Count).Delete

End If

Next i
Hence our complete code is here for second scenario

Sub secondScanarios()

Dim last_Row As Integer

Sheets("sheet2").Activate

'first count number of rows

last_Row = Range("A" & Rows.Count).End(xlUp).Row

'RemoveDuplicates fnction checks duplicate values inside sheet and remove automatically from sheet

Range("B2" & ":" & "D" & last_Row).RemoveDuplicates Columns:=Array(1), Header:=xlNo

'Second logic for delete duplicate rows completely

For i = 1 To last_Row

If Cells(i, 2).Value = "" Then

Rows(i & ":" & Rows.Count).Delete

End If

Next i

MsgBox "Done"

End Sub
And hence my final code is

Sub secondScanarios()

Dim last_Row As Integer

Sheets("sheet2").Activate

'first count number of rows

last_Row = Range("A" & Rows.Count).End(xlUp).Row

'RemoveDuplicates fnction checks duplicate values inside sheet and remove automatically from sheet

Range("B2" & ":" & "D" & last_Row).RemoveDuplicates Columns:=Array(1), Header:=xlNo

'Second logic for delete duplicate rows completely

For i = 1 To last_Row

If Cells(i, 2).Value = "" Then

Rows(i & ":" & Rows.Count).Delete

End If

Next i

MsgBox "Done"


End Sub

Note : To solve second scenario I used two steps to solve this problem so if you have better option then please write inside comment box for its proper solution.

Here is link of sheet which contains scenarios click here 

Comments

Popular posts from this blog

Secure Database Connectivity in node.js with mysql

Export data from mysql db to csv file using java

API (Application Programming Interface)