+ Reply to Thread
Results 1 to 7 of 7

udf to pull distinct values from independent columns in excel

  1. #1
    Registered User
    Join Date
    02-20-2023
    Location
    rome
    MS-Off Ver
    office 2021
    Posts
    15

    udf to pull distinct values from independent columns in excel

    Hello everybody. I need some help to write an udf in excel VBA that could pull a number from each of several columns (with elements in common) so that all pulled numbers are different. Let's say that the starting columns were A:[1,3,6], B:[2,4,5,6], C:[2,3,7], D:[1]. One possible solution could be [3,4,7,1]. The function has to consider dependancies between columns, in fact, in this case the number 1 can't be pulled from column A or else it won't be possible to pull anything from column D.
    to work around the problem i'm using a formula that calculates all possible combinations between columns and then filters for distinct values only. Then, i take a random solution using ARRAY and RANDBETWEEN formulas. this process, however, contains two big flaws:

    1-Once the number of combinations exceeds the row limit number (1048576), the formula returns an error.
    2-The formula calculates all possible solutions so it becomes increasingly slower as i add new columns.
    I'm looking for an udf using excel VBA that gets rid of these problems.

    In this worksheet i provide some examples to better understand the problem
    EXAMPLES.xlsm

    i already posted this question on reddit, but no one answered me yet. i would link the thread but the website is not letting me
    Attached Files Attached Files
    Last edited by MrBlonde_; 04-23-2023 at 01:52 PM.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: udf to pull distinct values from independent columns in excel

    Here's Sub Procedure, no udf.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    02-20-2023
    Location
    rome
    MS-Off Ver
    office 2021
    Posts
    15

    Re: udf to pull distinct values from independent columns in excel

    __________

  4. #4
    Registered User
    Join Date
    02-20-2023
    Location
    rome
    MS-Off Ver
    office 2021
    Posts
    15

    Re: udf to pull distinct values from independent columns in excel

    Quote Originally Posted by jindon View Post
    Here's Sub Procedure, no udf.
    Please Login or Register  to view this content.
    could you please explain how this works? thank you

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: udf to pull distinct values from independent columns in excel

    1) Please don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding

    2) The code;

    Reads the data from A1 to consecutive block of area and calculates all the combinations that have unique item in all columns using the elements in each column and randomly select one from them for solution.

    If you want to dig it, surf the net

    VBA ADO SQL etc.

  6. #6
    Registered User
    Join Date
    02-20-2023
    Location
    rome
    MS-Off Ver
    office 2021
    Posts
    15

    Re: udf to pull distinct values from independent columns in excel

    aright, thanks. I noticed you also set up the macro to show all possible combinations like i did in the example. i only put them because my old udf required it, but i don't need all of them. If that part slows down the function I'd remove it, but i don't know which lines to modify. Also, if the function calculates from column A to the right, would it be a problem if my range was let's say C1:F15? An udf would have been more fit to this situation because if i had a number outside of the range (in D18 if you take my example) i don't want the function to consider it
    Last edited by MrBlonde_; 04-27-2023 at 03:26 AM.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: udf to pull distinct values from independent columns in excel

    Please Login or Register  to view this content.

+ 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] Two columns unique distinct values to listbox
    By diegoasm11 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 03-24-2021, 05:33 AM
  2. [SOLVED] Count distinct values in two columns where the third matches a value
    By LLTom2021 in forum Excel Formulas & Functions
    Replies: 19
    Last Post: 02-20-2021, 07:54 AM
  3. Counting distinct values across columns
    By winkywright in forum Excel General
    Replies: 5
    Last Post: 12-09-2020, 09:35 AM
  4. Formula to compare two tables and pull distinct text values
    By markedwards in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-01-2020, 06:10 PM
  5. Pulling together distinct values from several columns
    By Asif75 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-11-2013, 12:09 PM
  6. Replies: 0
    Last Post: 08-03-2012, 04:31 PM
  7. count two columns for distinct values
    By warrencameron in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 07-18-2011, 09:09 AM

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