+ Reply to Thread
Results 1 to 7 of 7

Return r combinations from a group of n using Excel

  1. #1
    Registered User
    Join Date
    04-15-2019
    Location
    United Kingdom
    MS-Off Ver
    2017
    Posts
    3

    Return r combinations from a group of n using Excel

    Hi,

    I have numeric co-variates in groups of 1, 2, 3, 4, 5, 6, etc.

    Each group corresponds to a study I conducted

    I'm trying to find all possible combinations of each study's group of co-variates - for example, if study 1 has corresponding co-variates 8, 3, 7.... then I want to find 3C2... so (8, 3) would be the first 2character group; (8, 7) would be the 2nd 2character group; and so on...

    In Excel, I have my "studies" in row 1, and then the "groups of co-variates" corresponding to each study in row 2, and then I have the groups I want to combine them into (i.e., r=2, 3, 4, etc.) in the subsequent rows

    I'm wondering if there is a macro I could use for this?

    I'm attaching a sample of my data to give a better idea of exactly what I'm trying to achieve - I've manually created the combinations for "study 3".

    My data is quite large, and I'm really hoping to be able to create a UDF whereby I can just enter the function name (e.g., Combinations), select the range (i.e., the single group of co-variates corresponding to the study), and then enter the "r" I need

    Sorry if the above doesn't make sense, I'm quite new to VBA but think it may be my best option here! - feel free to ask any questions, all help is greatly appreciated as I really need to find a Macro to help me perform this function quickly!!!

    Thank you in advance

    Sample data.xlsx
    Last edited by Macros_2019; 04-17-2019 at 05:20 AM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Desperately need help writing Macro !!! :(

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however the thread title "Desperately need help writing Macro !!! " does not really convey what your request is about.

    I have changed it for you since you are a new member.

    In the future make sure that the title properly explains your request. Your title should be explicit and not be generic (this includes function names used without an indication of what you are trying to achieve).

    Please see Forum Rule #1 about proper thread titles.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,827

    Re: Find all combinations of a set of covariates for a study

    In response to PM (assuming your PM to me referenced this same problem). I might be able to help with something like this, but it is not the kind of programming that I do. There are probably others who are better at this than I am.

    I set up this spreadsheet for generating permutations, if it will help: https://www.excelforum.com/tips-and-...thout-vba.html
    I also would note that most algorithms that I see for generating permutations/combinations involve nested For..Next loops. Is there a specific part of generating these permutations/combinations that you are unsure of?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Registered User
    Join Date
    04-15-2019
    Location
    United Kingdom
    MS-Off Ver
    2017
    Posts
    3

    Re: Find all combinations of a set of covariates for a study

    Hey thank you for your reply!

    So I'm trying to generate combinations (not permutations, since order is not important) from a set of numbers I have listed in individual cells in excel.

    I found a VBA macro code online, but it involved an array formula and required me to list each co-variate out in separate cells (one under the other); and it ended up being as time consuming as manually listing the combinations myself :/

    I'm hoping it's possible to keep all co-variates corresponding to their individual study in one single excel cell, each separated by commas; and create a function using VBA so that I can yield nCr combinations in the cell underneath the group of co-variates. (Apologies if how I have worded this is confusing - this is why I attached a sample data file with an example of what I'm trying to achieve!)

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,827

    Re: Find all combinations of a set of covariates for a study

    From what I have seen, the basic coding procedure is the same for permutations and combinations -- nested For..Next loops with appropriate choice of the limits, so I'm still not sure what you need help with. For example, the combinations of "choose 2 different from 3" is essentially:
    Please Login or Register  to view this content.
    Or maybe your question is more about the incidental code that would go around the loops?

  6. #6
    Registered User
    Join Date
    04-15-2019
    Location
    United Kingdom
    MS-Off Ver
    2017
    Posts
    3

    Re: Find all combinations of a set of covariates for a study

    I'm sorry I'm completely new to VBA and macros and I haven't been able to find one that will achieve what I'm trying to do

    Would you be able to write a code for me that would produce the combinations I've listed in E4:E18, for example? *Please*

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,827

    Re: Return r combinations from a group of n using Excel

    Should be something like this (untested):
    Please Login or Register  to view this content.
    That would give you all of the r=2 combinations. Getting the r=3 combinations should be similar with a third For..Next loop added.

    Being new to VBA, you may want to spend some time with these programming structures. These are fairly common structures for many procedures and most programming languages:
    For..Next loops: https://www.wiseowl.co.uk/blog/s193/vba-for-next.htm
    arrays: https://excelmacromastery.com/excel-vba-array/
    Excel's object model (for accessing cells, ranges, sheets, etc.): https://msdn.microsoft.com/en-us/lib.../ff194068.aspx
    Split function: https://docs.microsoft.com/en-us/off...split-function
    half of programming is debugging, so you might as well become familiar with VBA's debugging tools: http://www.cpearson.com/Excel/DebuggingVBA.aspx

+ 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. desperately need help with IF function please
    By hendrilearning in forum Excel Formulas & Functions
    Replies: 19
    Last Post: 02-12-2015, 01:04 PM
  2. Desperately need help creating a macro based tool for work!
    By kylenls in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-03-2013, 10:56 AM
  3. Desperately Need help with If/Or/And formula
    By Ski352215 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-02-2013, 08:59 PM
  4. Desperately need help with a macro
    By scommar1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-06-2006, 07:30 AM
  5. Desperately need help!!
    By Paula_p in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 06-13-2006, 05:30 PM
  6. Desperately need help with 3 calculations
    By Scoooter in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-12-2006, 11:35 AM
  7. Help desperately required
    By andy callaghan in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-06-2005, 06:57 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