1

I'm a new user in VBA and I wrote the following basic code in order to eliminate duplicate data in one sheet and extract unique data into another sheet.

For example:

  1. in Sheet1 there are total number of 184 cells which contains the same date (12/07/2007) in Column A.
  2. I desire to take one of that date data and write it to a cell in Sheet2.
  3. Therefore, there will be 12/07/2007 written in A1 in Sheet2.

I would appreciate any kind of help.

Code:

Sub Take_Unique() Workbooks("historicaldata.xls").Activate Dim i As String Dim xrow As Long xrow = 2 Do Until Sheet1.Cells(xrow, 1) = "" If Sheet1.Cells(xrow, 1).Value = Sheet1.Cells(xrow + 1, 1) Then Sheet12.Cells(xrow, 1).Value = Sheet1.Cells(xrow, 1) End If xrow = xrow + 1 Loop End Sub 
5
  • 1
    What is the problem with your code as written? Commented Jun 9, 2016 at 10:31
  • It just eliminates one of the date data and 183 of them still remains. Commented Jun 9, 2016 at 10:38
  • 1
    Why not just use either the Remove Duplicates feature or the Advanced Filter? Commented Jun 9, 2016 at 10:40
  • Thanks! However, it is going to be a part of a longer code. So, VBA usage is necessary for me. Commented Jun 9, 2016 at 10:47
  • 1
    Either of those can be incorporated into code. Commented Jun 9, 2016 at 10:48

2 Answers 2

2

Another approach is my duplicate master addin.

It handles whitespace, case sensitivities and even regexp matches - so it goes beyond the default comparison.

Sign up to request clarification or add additional context in comments.

Comments

1

Simpler to just copy the whole column from sheet1 to sheet12:

Sheet1.Columns(1).Copy Destination:=Sheet12.Columns(1) 

Then use the RemoveDuplicates functionality to drop any duplicates (set the Header if it's present and not if it isn't)

Sheet12.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes 

2 Comments

Thank you very much Dave! Your answer helped me a lot.
Sorry, I am a newbie

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.