+ Reply to Thread
Results 1 to 11 of 11

Compare cells of an array to return only the unique values

Hybrid View

goodm009 Compare cells of an array to... 08-16-2013, 06:31 PM
xladept Re: Compare cells of an array... 08-16-2013, 07:24 PM
goodm009 Re: Compare cells of an array... 08-16-2013, 07:47 PM
goodm009 Re: Compare cells of an array... 08-16-2013, 07:48 PM
cytop Re: Compare cells of an array... 08-16-2013, 07:34 PM
xladept Re: Compare cells of an array... 08-16-2013, 07:53 PM
goodm009 Re: Compare cells of an array... 08-17-2013, 02:44 AM
xladept Re: Compare cells of an array... 08-17-2013, 01:48 PM
goodm009 Re: Compare cells of an array... 08-17-2013, 02:13 PM
Kalithro Re: Compare cells of an array... 08-17-2013, 02:57 PM
xladept Re: Compare cells of an array... 08-17-2013, 03:00 PM
  1. #1
    Registered User
    Join Date
    08-16-2013
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Compare cells of an array to return only the unique values

    Hi guys, I have an array of unknown size (at least 100x 85) , filled with alphanumeric characters and blank cells - basically around 100 lists of vaying lengths. I need to have a way to remove the duplicate cells from across all the lists, leaving only the unique values. And also get rid of the blank cells intermixed - optional. My thought process was to use nested loops to compare the A1 against every cell in array and delete if equal and move on if different, then start back at A2 and proceed on and on. This seems like the simplest solution to me, but I am not well versed in Excel, so I'm having a little difficulty. Here's my code so far, it compiles but does nothing lol

    Option Explicit
    Sub test()
    Dim varSheetA As Variant
    Dim strRangeToCheck As String
    Dim iRow As Long
    Dim iCol As Long
    Dim iRows As Long
    Dim iCols As Long
    Dim iCole As Long
    ' Dim iRowe As Long
    ' Dim iRowes As Long
    ' Dim iColes As Long


    'Range("A1:DD100").SpecialCells(xlCellTypeBlanks) = "Blank" 'Delete blanks -> sort by by "Blank and delete

    strRangeToCheck = "A1:DD100"

    varSheetA = Worksheets("Sheet1").Range(strRangeToCheck)

    For iRows = LBound(varSheetA, 1) To UBound(varSheetA, 1)
    ' iRowes = iRows + 1

    For iCols = LBound(varSheetA, 2) To UBound(varSheetA, 2)
    ' iCole = iCols + 1

    For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
    ' iRowe = iRow + 1

    For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
    iCole = iCol + 1

    If iCole < 100 Then

    If varSheetA(iRows, iCols) = varSheetA(iRow, iCole) Then ' Cells are identical.
    varSheetA(iRow, iCole) = " "
    Else ' Cells are different.

    End If
    End If

    Next iCol
    Next iRow
    Next iCols
    Next iRows

    End Sub

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Compare cells of an array to return only the unique values

    Hi Goodm,

    Try this:

    Sub Goodm(): Dim A, r As Long, c As Long
    Dim p As Long, q As Long
    A = Range("A1:DD100")
    For r = 1 To 99: For c = 1 To 107
    For p = r + 1 To 100: For q = c + 1 To 108
    If A(r, c) <> "" Then
    If A(p, q) = A(r, c) Or A(p, q) = " " Then
    A(p, q) = ""
    End If: End If
    Next q: Next p: Next c: Next r
    Range("A1:DD100") = A
    End Sub
    BTW - You need to put code tags around your code (just highlight the code and click the #)
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    08-16-2013
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Compare cells of an array to return only the unique values

    this is VERY close!. it misses quite a few repeats tho.

  4. #4
    Registered User
    Join Date
    08-16-2013
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Compare cells of an array to return only the unique values

    this is VERY close!. it misses quite a few repeats tho.


    Quote Originally Posted by xladept View Post
    Hi Goodm,

    Try this:

    Sub Goodm(): Dim A, r As Long, c As Long
    Dim p As Long, q As Long
    A = Range("A1:DD100")
    For r = 1 To 99: For c = 1 To 107
    For p = r + 1 To 100: For q = c + 1 To 108
    If A(r, c) <> "" Then
    If A(p, q) = A(r, c) Or A(p, q) = " " Then
    A(p, q) = ""
    End If: End If
    Next q: Next p: Next c: Next r
    Range("A1:DD100") = A
    End Sub
    BTW - You need to put code tags around your code (just highlight the code and click the #)

  5. #5
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Compare cells of an array to return only the unique values


  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Compare cells of an array to return only the unique values

    See if the Trim and case catches the rest:

    Sub Goodm(): Dim A, r As Long, c As Long
    Dim p As Long, q As Long
    A = Range("A1:DD100")
    For r = 1 To 99: For c = 1 To 107
    For p = r + 1 To 100: For q = c + 1 To 108
    If A(r, c) <> "" Then
    If Trim(LCase(A(p, q))) = Trim(LCase(A(r, c))) Or A(p, q) = " " Then
    A(p, q) = ""
    End If: End If
    Next q: Next p: Next c: Next r
    Range("A1:DD100") = A
    End Sub
    Last edited by xladept; 08-16-2013 at 08:05 PM.

  7. #7
    Registered User
    Join Date
    08-16-2013
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Compare cells of an array to return only the unique values

    thanks!!! it still doesnt get rid of all the duplicates but it definitely better than where i was going. I'm pretty sure i can make this work for my needs. thanks alot again!!

    Quote Originally Posted by xladept View Post
    See if the Trim and case catches the rest:

    Sub Goodm(): Dim A, r As Long, c As Long
    Dim p As Long, q As Long
    A = Range("A1:DD100")
    For r = 1 To 99: For c = 1 To 107
    For p = r + 1 To 100: For q = c + 1 To 108
    If A(r, c) <> "" Then
    If Trim(LCase(A(p, q))) = Trim(LCase(A(r, c))) Or A(p, q) = " " Then
    A(p, q) = ""
    End If: End If
    Next q: Next p: Next c: Next r
    Range("A1:DD100") = A
    End Sub

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Compare cells of an array to return only the unique values

    Hi Goodm,

    I actually thought about this in the interim and here's what I've come up with - see if it's good

    Sub Goodm(): Dim A, r As Long, c As Long
                    Dim p As Long, q As Long
                        A = Range("A1:DD100")
        For r = 1 To 99: For c = 1 To 107
        If A(r, c) = "" Then GoTo GetNext
        
            For q = c + 1 To 108
    If Trim(LCase(A(r, c))) = Trim(LCase(A(r, q))) _
    Or A(r, q) = " " Then A(r, q) = ""
            Next q
    
            For p = r + 1 To 100
    If Trim(LCase(A(r, c))) = Trim(LCase(A(p, c))) _
    Or A(p, c) = " " Then A(p, c) = ""
            Next p
    
    GetNext: Next c: Next r
    
    Range("A1:DD100") = A
    End Sub

  9. #9
    Registered User
    Join Date
    08-16-2013
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Compare cells of an array to return only the unique values

    that got it. awesome! thanks!

    Quote Originally Posted by xladept View Post
    Hi Goodm,

    I actually thought about this in the interim and here's what I've come up with - see if it's good

    Sub Goodm(): Dim A, r As Long, c As Long
                    Dim p As Long, q As Long
                        A = Range("A1:DD100")
        For r = 1 To 99: For c = 1 To 107
        If A(r, c) = "" Then GoTo GetNext
        
            For q = c + 1 To 108
    If Trim(LCase(A(r, c))) = Trim(LCase(A(r, q))) _
    Or A(r, q) = " " Then A(r, q) = ""
            Next q
    
            For p = r + 1 To 100
    If Trim(LCase(A(r, c))) = Trim(LCase(A(p, c))) _
    Or A(p, c) = " " Then A(p, c) = ""
            Next p
    
    GetNext: Next c: Next r
    
    Range("A1:DD100") = A
    End Sub

  10. #10
    Forum Contributor
    Join Date
    08-14-2013
    Location
    Florida
    MS-Off Ver
    Excel 2013
    Posts
    105

    Re: Compare cells of an array to return only the unique values

    How about something like this. Adust code as needed for range required. This loops though range and stores all cells values. Then places values in Sheet2. Then uses Advanced Filter to get only uniques. Macro is setup to use "Sheet1" and "Sheet2".

    Sub GetUnique()
    
    Dim TnFv(1 To 10000000) As String, p As Long
    Dim remME As Long
    Dim x As Long
    Dim y As Long
    
    
    
    ThisWorkbook.Sheets("Sheet2").Columns("A:B").Clear
    
    For x = 1 To 100
        For y = 1 To 108
            p = p + 1
            TnFv(p) = ThisWorkbook.Sheets("Sheet1").Cells(x, y).Value
        Next y
    Next x
    
    remME = p
    
    For p = 1 To remME
        ThisWorkbook.Sheets("Sheet2").Cells(p, 1).Value = TnFv(p)
    Next p
    
    Application.CutCopyMode = False
    Columns("A:A").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Columns( _
            "B:B"), Unique:=True
    
    End Sub
    Last edited by Kalithro; 08-17-2013 at 03:01 PM.

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Compare cells of an array to return only the unique values

    You're welcome - I'm glad that all that gray matter matters!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. add to array formula to only return unique values
    By jason892 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-11-2013, 06:39 AM
  2. [SOLVED] Array formular - Return only unique values
    By jackyong1985 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-20-2012, 07:17 AM
  3. Identify mins in array for each unique key and return to new worksheet
    By agabbi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-09-2009, 02:23 PM
  4. Compare and copy unique values
    By sa02000 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-12-2006, 04:45 PM
  5. [SOLVED] Compare 2 columns for unique values
    By Jim Gregg in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-10-2006, 12:40 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1