+ Reply to Thread
Results 1 to 27 of 27

Generate Random Char "x" in Range with Condition

  1. #1
    Forum Contributor
    Join Date
    05-20-2016
    Location
    Detroit
    MS-Off Ver
    Office 365 Version 2008 (July 2020)
    Posts
    819

    Generate Random Char "x" in Range with Condition

    Hi,

    Please see attached

    I have an Range with N rows and X Columns
    I want to Randomize N in Range this way :
    - Each N appear Y times Horizontal (in any X Columns)
    - Each N appear Minimum times in Vertical

    Example : N=19 (rows) , X=5 (columns) , Y=3 (condition)
    Each N (1,2,3,4, 19) must appear y=3 times in any x=5 Columns
    This is Horizontal

    But Vertical in Range is tricky because I want to have Minimum of N on each Column X (see attached for this)
    Minimum come from the fact that each N appear 3 times but not in 3 columns but in 5 columns in my example
    Attached Files Attached Files
    Last edited by ionelz; 10-23-2022 at 09:21 AM.

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,173

    Re: Math Question in Excel

    Please Login or Register  to view this content.
    Attached Files Attached Files
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,792

    Re: Math Question in Excel

    Administrative Note:

    ANOTHER GENERIC THREAD TITLE

    We would very much like to help you with your query, however the thread title does not really convey what your request is about. Tell us what you are trying to do, not how you think it should be done.

    Please take a moment to amend your thread title. 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 and adjust accordingly. To edit the thread title, open the original post to edit and then click on Go Advanced (bottom right) to access the area where you can edit your title.

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  4. #4
    Forum Contributor
    Join Date
    05-20-2016
    Location
    Detroit
    MS-Off Ver
    Office 365 Version 2008 (July 2020)
    Posts
    819

    Re: Math Question in Excel

    Thank you so much, please see attached
    That is what I was looking for
    The results a very close but not exactly
    So Horizontal is fine, they are Y=3 in all rows
    But Vertical I should have them EQUAL when M6 is Integer and combination of Int(M6) and Integer(M6)-1 when M6 is not Integer
    Attached Files Attached Files
    Last edited by ionelz; 10-23-2022 at 09:47 AM.

  5. #5
    Forum Contributor
    Join Date
    10-22-2022
    Location
    Santa Cruz, Bolivia
    MS-Off Ver
    MSO 365
    Posts
    157

    Re: Generate Random Char "x" in Range with Condition

    hi there, after check the consult, I propose this possible solution to copy and drag from C3:

    Please Login or Register  to view this content.
    Hope was usefull.

  6. #6
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Generate Random Char "x" in Range with Condition

    In your example, if you're picking any 3 from 5 then you can find out how many options there are for this with:

    Please Login or Register  to view this content.
    Which is the same as 5!/(3!*(5-3)!) = 120/6*2 = 120/12 = 10. Your possibilities for putting these in rows are:

    11100, 11010, 11001, 10110, 10101, 10011, 01110, 01101, 01011, 00111

    So perhaps you could write code to:

    1. Generate all the possible combinations
    2. Randomly place one of these values in the grid and remove it from the set
    3. Move to the next row and repeat
    4. If the set it empty, re-generate it

    WBD
    Office 365 on Windows 11, looking for rep!

  7. #7
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Generate Random Char "x" in Range with Condition

    You'd have to extend on this for more cases but I wrote this in a hurry:

    Please Login or Register  to view this content.
    Clear the grid then run the FillGrid macro.

    WBD

  8. #8
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,173

    Re: Generate Random Char "x" in Range with Condition

    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    05-20-2016
    Location
    Detroit
    MS-Off Ver
    Office 365 Version 2008 (July 2020)
    Posts
    819

    Re: Generate Random Char "x" in Range with Condition

    Sir, SUPER THANK YOU
    Looking now and so far so good, I will let you know
    I found so far one think that, Matrix Range is not defined correct
    I mean because N change, bottom right corner of Matrix should "live" change too

    Also please look into Set rng = Range("B3:G" & lrow)
    It should NOT be G, it again should be "B + X"
    If x=5 then yes is G but if X=4 then is F
    Also I will make sure in my input data validation that always Y<=X
    Last edited by AliGW; 10-24-2022 at 04:36 AM. Reason: Please DON'T quote unnecessarily!

  10. #10
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,173

    Re: Generate Random Char "x" in Range with Condition

    Maximum value of X ?

    Will look at this tomorrow.

  11. #11
    Forum Contributor
    Join Date
    05-20-2016
    Location
    Detroit
    MS-Off Ver
    Office 365 Version 2008 (July 2020)
    Posts
    819

    Re: Generate Random Char "x" in Range with Condition

    Checked and all looks good
    Max X is 5 and Min X is 3
    Also Y <=X but I will take care of X and Y in my data validation
    Last edited by AliGW; 10-24-2022 at 04:36 AM. Reason: Please DON'T quote unnecessarily!

  12. #12
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,173

    Re: Generate Random Char "x" in Range with Condition

    Please Login or Register  to view this content.
    Attached Files Attached Files

  13. #13
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Generate Random Char "x" in Range with Condition

    I'd started on this so I'll post what I did. Not trying to tread on toes but it might be useful for someone else in the future. Fill in the values, click "Generate" and it will create a new sheet with the table.

    WBD
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    05-20-2016
    Location
    Detroit
    MS-Off Ver
    Office 365 Version 2008 (July 2020)
    Posts
    819

    Re: Generate Random Char "x" in Range with Condition

    What you did is indeed very cool !
    I would like to thank you for that
    All work as requested

    However since you create a NEW Tab it create a little "problem" for me, for the next step
    What I would like next to do is this :
    01. In your Tab Inputs, say at Y3:Y100 to have N and next to each N (so at Z3:Z100) a Name (name1, name2.....,nameN) I will create this
    02. On Tab Inputs at say I2:M2 to have 1,2,3,4,X
    03. And now at I3, which is column X=1, "List" consecutive, not the "x" from generated Sheet1 X=1, but ALL the Name from Z with corresponded N
    Sorry !
    So say in generated Tab we have marked "x" in column X=1 this N : 1,5,8,9,10....I want names which correspond to these N's listed consecutive

  15. #15
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,173

    Re: Generate Random Char "x" in Range with Condition

    Please post a sample workbook.

  16. #16
    Forum Contributor
    Join Date
    05-20-2016
    Location
    Detroit
    MS-Off Ver
    Office 365 Version 2008 (July 2020)
    Posts
    819

    Re: Generate Random Char "x" in Range with Condition

    I was able to solve my needs but ONLY if you could help me again !
    Generate create new Sheets
    I super like the idea of create a new sheet at Generate but could you please do a modification so it always overwrite in Sheet1

  17. #17
    Forum Contributor
    Join Date
    05-20-2016
    Location
    Detroit
    MS-Off Ver
    Office 365 Version 2008 (July 2020)
    Posts
    819

    Re: Generate Random Char "x" in Range with Condition

    Quote Originally Posted by ExceLogan View Post
    hi there, after check the consult, I propose this possible solution to copy and drag from C3:

    Please Login or Register  to view this content.
    Hope was usefull.
    Please see explanation, I have attached file again
    Attached Files Attached Files

  18. #18
    Forum Contributor
    Join Date
    05-20-2016
    Location
    Detroit
    MS-Off Ver
    Office 365 Version 2008 (July 2020)
    Posts
    819

    Re: Generate Random Char "x" in Range with Condition

    Quote Originally Posted by WideBoyDixon View Post
    I'd started on this so I'll post what I did. Not trying to tread on toes but it might be useful for someone else in the future. Fill in the values, click "Generate" and it will create a new sheet with the table.

    WBD
    Could you please do a modification ?
    At Generate it create new Sheets (Sheet1, Sheet2 ....Sheetn)
    I want to use the data from Sheet1 so I want to write my formulas relative to Sheet1
    Could you please overwrite at Generate only in Sheet1 ?
    This is regarding file attached at post #13
    Thank you
    Last edited by ionelz; 10-26-2022 at 01:28 PM.

  19. #19
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Generate Random Char "x" in Range with Condition

    I've adapted the macro in the attached. You'll need to run FillGrid().

    WBD
    Attached Files Attached Files

  20. #20
    Forum Contributor
    Join Date
    05-20-2016
    Location
    Detroit
    MS-Off Ver
    Office 365 Version 2008 (July 2020)
    Posts
    819

    Re: Generate Random Char "x" in Range with Condition

    Quote Originally Posted by WideBoyDixon View Post
    I've adapted the macro in the attached. You'll need to run FillGrid().

    WBD
    Please see attached
    This is my last try !
    I do not want to bug you with this , I am sure you have to answer to may others
    Thank you so much for your effort

    - Matrix is N x A cells
    - Fill with "x", N x B cells(B<=A)
    Required :
    - Each N have B "x" (each Row have B "x")
    - Each Column (each A) have N x B /A "x"

    In words :
    A group of N people need to work B (B<=A) days out of A days (A=1,2,3,4,5), so max A is 5
    How to spread them in such a way that they are MINIMUM number per day
    Attached Files Attached Files
    Last edited by ionelz; 10-30-2022 at 07:48 PM.

  21. #21
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Generate Random Char "x" in Range with Condition

    OK. Trying to distribute the "x" in the columns adds an extra layer of complexity that I don't think can be easily solved. I can't even begin to think how you would solve that. If you don't want random answers then it might be possible to hard-code it but with the current solution, I can't help.

    WBD

  22. #22
    Forum Contributor
    Join Date
    05-20-2016
    Location
    Detroit
    MS-Off Ver
    Office 365 Version 2008 (July 2020)
    Posts
    819

    Re: Generate Random Char "x" in Range with Condition

    Well, but please see and test your post #13 which works absolutely PERFECT, for any N, A, or B !!!!
    That is exactly what I want
    Except, that it create new Sheet for every Generate
    That is cool too, but all the modification I need to post #13 is to Generate in Sheet1 and then overwrite there in Sheet1 regardless of how may times I press Generate
    Last edited by ionelz; 10-31-2022 at 10:14 AM.

  23. #23
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Generate Random Char "x" in Range with Condition

    That's what I posted in post #19

    WBD

  24. #24
    Forum Contributor
    Join Date
    05-20-2016
    Location
    Detroit
    MS-Off Ver
    Office 365 Version 2008 (July 2020)
    Posts
    819

    Re: Generate Random Char "x" in Range with Condition

    Ok, maybe I am doing something wrong, but I can not make #19 work.
    In #19 if for example I make N=30 nothing happen from row 20
    #13 was super, I will try to work with
    Thank you

  25. #25
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Generate Random Char "x" in Range with Condition

    OK. Once that try then

    I've changed this so it auto-generates when you change any of the parameters.

    WBD
    Attached Files Attached Files

  26. #26
    Forum Contributor
    Join Date
    05-20-2016
    Location
    Detroit
    MS-Off Ver
    Office 365 Version 2008 (July 2020)
    Posts
    819

    Re: Generate Random Char "x" in Range with Condition

    Thank you so much ! All done now
    Maybe one more thing if possible : lost Undo
    Last edited by ionelz; 11-01-2022 at 06:01 AM.

  27. #27
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Generate Random Char "x" in Range with Condition

    You can't undo macros.

    WBD

+ 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. Excel Math Question
    By sjalil22 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-23-2022, 08:34 PM
  2. Excel math/formula question
    By pawnia in forum Excel General
    Replies: 2
    Last Post: 01-04-2019, 05:06 PM
  3. Replies: 9
    Last Post: 11-25-2018, 03:50 PM
  4. [SOLVED] Excel/Math question
    By natetheblade in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-19-2013, 04:56 PM
  5. An Excel math question
    By nohotash in forum Excel General
    Replies: 4
    Last Post: 03-05-2010, 01:54 PM
  6. How do I ? math/excel question
    By Madduck in forum Excel General
    Replies: 3
    Last Post: 07-26-2006, 12:45 AM
  7. Replies: 3
    Last Post: 02-16-2006, 07:00 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