+ Reply to Thread
Results 1 to 37 of 37

Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique values.

  1. #1
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique values.

    Hello,

    How do I find the unique CSV values for the used range between columns B, C, D (could be any difference between the CSV and any set of data).Many values are common between columns but I only need the unique values.

    Example given in spreadsheet attached. Column D to populated.

    Can formula do this - if not what is the VBA code?

    If this was 2 columns how can I adjust.


    Thank you,
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Dang, that was not easy. There has to be a better way to do this, so if any of you gurus want to share it, I'm interested. However, this should work:
    Please Login or Register  to view this content.
    1. Include a sample workbook with an example of the result you want
    2. Use [CODE] and [FORMULA] wrappers for your VBA code or excel formulas
    3. If your question has been answered, mark the thread as SOLVED and click on the "Add Rep" star to thank whoever helped you.

  3. #3
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Thankyou, sorry if it was too difficult.

    Does this compare columns B, C, D or B and C (flexible) then populate column E?

    I meant populate column E above not D. Does its fill down and populate the used range. There could be more than 1 comparison line?

    Column A is not part of the comparison.

    Thankyou,

  4. #4
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Yes, this will compare all data except in column A and populate the next available column for each row. If you want it to always populate column E, it can, but I wrote it in case you had more than 3 lists to compare. The only assumption I made is that numbers are not repeated within a single list. If that's not true, I can modify the code to check for duplicates inside the same list too.
    Last edited by k64; 06-05-2014 at 10:52 AM.

  5. #5
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Hello Thankyou, I tried to run this and it had an error subscript out of range (Error 9).

    Any ideas?

    Sounds great to be that flexible say if I had 2 columns(B and C) I take it would populate column D automatically?

    Thanks,

  6. #6
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    I just realized a better solution. This is more concise, and can deal with duplicates within a single list.
    Please Login or Register  to view this content.
    In terms of processing speed, I think that it could be made faster in theory, if I only tested items that I hadn't yet encountered, but unless you are dealing with huge data sets, there won't be any noticeable difference.
    Last edited by k64; 06-05-2014 at 02:11 PM.

  7. #7
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Thankyou but once run this I get the same error 9 as per post 5 above?

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    My code assumes columns B-C and the outcome is in column E. It first clears what ever in column E is and re-populate with new data. If you just want 2 columns ONLY, I can easily change it.
    Please note the code should run it on sheet1, otherwise, you will get out of range error.
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Thankyou, it stops at this line :CreateObject("Scripting.Dictionary”).


    Can you make it flexible 2, 3 or 4 columns and populate the last column?

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    I dread to imagine you are using Mac, not PC. The code will not work on Mac

  11. #11
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Sorry, I am using mac but i have excel 2011 installed. It will be used on a pc though.

    Does this explain why k64 code had a error 9 error?

    Please can your code be tweaked to work on flexible column comparison (b&c - 2 columns) (b,c,d - 3 columns), thanks

    Also, can you tweak the code so that it populates values not found in columns B but found in columns C or D(could be the same values in both columns C and D I.E 110 found in columns C and D but not found in column B) or (like wise 110 could be found in columns C and not D and not B either?

    Column B is the base values . need to find out what needs to be added (i.e any values not found in column B) and is shown in the other columns.

    Is the code flexible to columns. If not how do i change?

    Thankyou very much,

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Sorry!
    "Also, can you tweak the code so that it populates values not found in columns B but found in columns C or D(could be the same values in both columns C and D I.E 110 found in columns C and D but not found in column B) or (like wise 110 could be found in columns C and not D and not B either?"
    I do not understand! The code has been already very complicated.
    Yes, I can make the code flexible to cope with any column.

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    If you want to run on 2 columns(B and C), the simplest thing to do is to clear column D(Not delete), the code works. If you want to run on 3 columns, leave as it is. See attached.
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    I am sorry for being unclear, I kind of need two separate similar codes:

    1st one - unique between columns from columns B as above (flexible populate last column - adjacent to the part in column A).
    2nd one - column B is base values (identify values found in adjacent column C and/or D BUT NOT FOUND FOR THE ADJACENT record in column B) populate in last column as above (flexible)

    Sorry if this is all too complicated.

    Thankyou,
    Last edited by grphillips; 06-05-2014 at 05:19 PM.

  15. #15
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Can you post examples of what you want the results to be for each?

  16. #16
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Evening Gents, thankyou, please find attached 2 outputs.

    Was the only reason I had that run time error 9 because I am using a MAC?

    I run this on sheet 1 in excel 2011 on a mac.

    Output 2 is most important output.

    It will be run on Excel 2010.

    Many thanks.
    Attached Files Attached Files

  17. #17
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    in response to your post #1
    does this code give what you want? it should run OK on a Mac as well as a PC
    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Output 1: I think it works for 3 columns(not 2 or 4 flexible) but only as far as line 198 when it says out of range error message. If I copy down more values the out of range error message appears?

    Does anyone have any ideas for output 2?

    Thankyou Gents.
    Last edited by grphillips; 06-05-2014 at 06:44 PM.

  19. #19
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    This produces output 2 for me. Again, there might be a prettier way to do this, but it works (at least on PC).
    Please Login or Register  to view this content.

  20. #20
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Quote Originally Posted by grphillips View Post
    Output 1: I think it works for 3 columns(not 2 or 4 flexible) but only as far as line 198 when it says out of range error message. If I copy down more values the out of range error message appears?

    Does anyone have any ideas for output 2?

    Thankyou Gents.
    1. "I think it works for 3 columns" You think? Does it or doesn't it?
    2. "not 2 or 4 flexible". You only asked for 3 columns in Post#1 so that's all the code did. Flexibility is easy if you'd like to be more specific
    3. There's nothing in that code which should cause anything untoward to happen at line 198. Without seeing your data it's hard to say more. I copied your Post#1 data down 1000 rows, ran the code, and it all worked fine for me.
    4. Are any of your numbers greater than 500 or less than 100? If so, are you able to specify their feasible range?

    Anyway, here's a modified code. "Flexibility" as indicated in red near the top.
    Run it on the same dataset that gave you the 198th row error.
    Does it still give the same error, or does it just give blanks (after row 197 or 198) in the column in which you want the output?
    Please Login or Register  to view this content.
    Last edited by kalak; 06-05-2014 at 07:40 PM.

  21. #21
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Could you please replace the code in my above post by
    Please Login or Register  to view this content.
    (comes from modification without enough time for testing)

  22. #22
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    I've posted 3 solutions, AB33 has posted 2, and kalak has posted 2. If it still doesn't work it's time to 1. Try running it on a pc, since that's where it will be run and 2. post a more robust sample that looks like your actual data.

  23. #23
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Hello,

    Thank you all. Here is some feedback:

    Output 1:

    Kalak - code worked when I posted in sheet 1 but not in module?
    K64 - thank you, sorry to say this did not work. It stopped at the worksheet function when I pasted in sheet 1. It did not work in module as well. Admittently I am not that knowledgable of functions when to paste in sheet 1 and when to paste in module. Not sure if you expected me to do something specific before hand to get this set up. Thought it would be easier to paste in module so it could be used anywhere?
    AB33 - sorry will get back to you on this next Mon - didn’t get chance.

    Output 2:

    Gents, do you have any code to try for this output in post 16?

    Thanks,

  24. #24
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Here is the first example you gave, with both my solutions. Notice that both produce the desired output. I also attached output 2 with my solution. Notice that it also produces the desired output.
    Attached Files Attached Files

  25. #25
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Thank you give me till Monday ad I should have more feedback on PC.

    Testing on my MAC here:

    Output 1 3 cells - when I run the macro find unique it stops at the below with run time error 9 saying out of range. Is this because it is MAC. I am using Microsoft excel 2011 on it.

    If UBound(Filter(arr, arr(y))) = 0 Then


    Output2 - when I Run find unique sub included within I get error 9 out of range message at:

    If (Not UBound(Filter(arr(0), arr(x)(i))) > -1) And Not UBound(Filter(uniq, arr(x)(i))) > -1 Then .


    Any ideas why?

    Thankyou,

  26. #26
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    I'm sorry, I have no experience with Macs. Let me know how it works on a PC.

  27. #27
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Over 200 views thats pretty popular for a thread!

    Question: Why does Output 1 have 3 sets of code within – Sub Find_Unique and Sub_Find Unique 2 and then a function, what’s the difference?

    Output 2 has 1 Sub only but no function?


    Output 1

    Sub find_unique()
    Dim arr() As Variant
    Dim uniq As String
    col = WorksheetFunction.CountA(Range("2:2")) - 2
    ReDim arr(0 To col)
    For a = 2 To WorksheetFunction.CountA(Range("A:A"))
    For x = 0 To col
    arr(x) = Split(Cells(a, x + 2).Value, ";")
    Next x
    For x = 0 To col
    For i = 0 To UBound(arr(x))
    isuniq = True
    For y = 0 To col
    If y <> x Then isuniq = isuniq And (Not UBound(Filter(arr(y), arr(x)(i))) > -1)
    Next y
    If isuniq Then uniq = uniq & ";" & arr(x)(i)
    Next i
    Next x
    Cells(a, col + 3).Value = uniq
    uniq = ""
    Next a

    End Sub


    Sub find_unique2()
    Dim list$, uniq() As String, arr() As String
    Dim a&, x&, y&, col&, r As Long
    col = Sheets(1).UsedRange.Columns.Count
    r = WorksheetFunction.CountA(Range("A:A"))

    ReDim uniq(2 To r)
    For a = 2 To r
    list = ""
    For x = 2 To col
    list = list & ";" & Cells(a, x).Value
    Next x
    arr = Split(list, ";")
    For y = 1 To UBound(arr)
    If UBound(Filter(arr, arr(y))) = 0 Then uniq(a) = uniq(a) & ";" & arr(y)
    Next y
    Next a
    Cells(2, col + 1).Resize(r - 1, 1).Value = uniq

    End Sub

    Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
    End Function


    Output 2:

    Sub find_unique()
    Dim arr() As Variant
    Dim uniq() As String
    col = WorksheetFunction.CountA(Range("2:2")) - 2
    ReDim arr(0 To col)

    For a = 2 To WorksheetFunction.CountA(Range("A:A"))
    j = 0
    For x = 0 To col
    arr(x) = Split(Cells(a, x + 2).Value, ";")
    c = c + UBound(arr(x)) + 1
    Next x
    ReDim uniq(0 To c - 1)
    For x = 1 To col
    For i = 0 To UBound(arr(x))
    isuniq = True
    If (Not UBound(Filter(arr(0), arr(x)(i))) > -1) And Not UBound(Filter(uniq, arr(x)(i))) > -1 Then
    uniq(j) = arr(x)(i)
    j = j + 1
    End If
    Next i
    Next x
    ReDim Preserve uniq(0 To j)
    Cells(a, col + 3).Value = Join(uniq, ";")

    Next a

    End Sub
    Last edited by grphillips; 06-06-2014 at 05:12 PM.

  28. #28
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Find unique 1 and find unique 2 are both solutions to your initial sample. They basically do the same thing, but the second one is a bit prettier from a coding standpoint. Use whichever you like best. The function is unnecessary. I was using it in a draft of my answer and then ended up taking it out. You can delete it. For output 2, find_unique should produce the same result you gave.
    Note that Output 1 is different than the first sample you gave. My first two functions work on the first example and may or may not work on output 1.

  29. #29
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    The desired results for Output 2 is different though from output 1?

    The coding should be different?
    Last edited by grphillips; 06-06-2014 at 05:30 PM.

  30. #30
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Yes. You have posted 3 files:
    1. CSV unique values between 3 cells.xlsx‎
    2. Output1.xlsx
    3. Output2.xlsx

    My 1st 2 solutions were designed for #1 and my third solution was for #3.

  31. #31
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Thanks. Will a worksheet function error appear for output 2 as there is no Function?

  32. #32
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    No. They work perfectly fine on my PC, just as they are. The function is not necessary at all.

  33. #33
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Hi,

    1 more point , can i paste this in a module myself to work on any spreadsheet? That’s my intention. Do I need to do anything else?

    I thought you would have to set the objects?


    I need it to work on any spreadsheet - not just this?

    Many Thanks,

  34. #34
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    My first and third solutions are set to work on the active sheet. My 2nd solution has a place in it where it says Sheets(1). You can change this to ActiveSheet. Just insert a new module and put the code in there and it should work.

  35. #35
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Yay!! this worked. 1 of the most viewed threads on the forum - excellent almost common use - excellent usability by others. Has to be 5 star!

    I will keep any eye out as I do more tests especially on large amounts of data.

    I did a little testing on small amounts of dat and this seems fine.


    Thanks all very much,
    Last edited by grphillips; 06-09-2014 at 02:00 PM.

  36. #36
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    You're welcome. Glad I could help Let us know if you need anything else, and +Rep the people who helped you out.

  37. #37
    Forum Contributor
    Join Date
    11-01-2004
    Posts
    145

    Re: Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique val

    Guys sorry for being slow to respond. Big thanks to everyone!!! Can I add any special rating to everyone. I thought I added 5 star to the post.

    I appreciate working out differences is never easy. Difficult coding but same time I imagine quite a lot of people would find the thread of use as the request was not too specific and pretty common in use.

+ 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. [SOLVED] Find count of Unique or Duplicate Values based on Concatenated values in 2 columns
    By bdicarlo1 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 12-03-2014, 12:42 AM
  2. [SOLVED] Find unique values from range ( populated cells only) & not count zeros or NA errors VBA
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-12-2014, 06:50 AM
  3. [SOLVED] find unique values in a range that contains text and empty cells
    By bombicci in forum Excel General
    Replies: 4
    Last Post: 07-04-2012, 09:17 AM
  4. [SOLVED] How to find all unique, matching values betwen two arrays of cells
    By exmonkey in forum Excel General
    Replies: 11
    Last Post: 06-20-2012, 05:47 PM
  5. Find Unique Values In A Range
    By nevi in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-10-2008, 04: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