+ Reply to Thread
Results 1 to 27 of 27

Improve number combination vba

  1. #1
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Improve number combination vba

    Hello, Friends,

    The following vba is used to combine "n" numbers (written in column A), picked "p" by "p" (written in cell "B1").

    Please Login or Register  to view this content.
    The Windows Task Manager shows that the memory usage is only 260MB !!!!

    So, I would like to ask your help to improve this code in order to INCREASE THE MEMORY USAGE.

    Thanks,

    John
    Last edited by JOAO12; 07-28-2018 at 10:15 AM.

  2. #2
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,625

    Re: Improve number combination vba

    Pl upload sample data showing what is required result.
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  3. #3
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,625

    Re: Improve number combination vba

    Pl upload sample file with data and also showing what is required result.

  4. #4
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Improve number combination vba

    Hello, kvsrinivasamurthy,

    Here is the file sample.

    As I said before, Windows Manager Task shows that the memory usage is only 260MB.

    I need to change the code in order to increase the memory usage and, consequently, increase the speed of the macro.

    Regards,

    John
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Improve number combination vba

    Why would like to "to INCREASE THE MEMORY USAGE."
    Is it for speed reason?
    If yes you could speed up your code, removing all .select and use
    Please Login or Register  to view this content.
    The memory size could be an issue because your code is prepared to have a lot of columns: Sheet added when the number of columns is reached
    - Battle without fear gives no glory - Just try

  6. #6
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Improve number combination vba

    Dear PCI,

    My PC has 16GB, windows and excel are 64-bit.

    I just do not know why the memory size is only 260MB. When I run other macros, the memory used is 11GB.

    Are there any improvements to this macro?

    Regards,

    John

  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Improve number combination vba

    Are there any improvements to this macro?
    Yes come back to my comments

    Try next code
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Improve number combination vba

    Dear PCI,

    Even with the changes you suggested, the memory used is still low 260-290 MB.

    Do you have any other suggestion?

    Regards,

    John

  9. #9
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Improve number combination vba

    The purpose of the changes was not to modify the memory used but only to speed up the macro: Is there some improvements ?
    It is possible to make the macro to go really faster rebuiling the code and using arrays which means memory ... this is the second step, but do you need it ?
    Last edited by PCI; 07-28-2018 at 03:37 PM.

  10. #10
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Improve number combination vba

    Dear PCI,

    If you could help me rebuild the code and make the macro go faster, I would be grateful.

    Regards,

    John

  11. #11
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Improve number combination vba

    I will see when I can do.
    Do you know that just for a 4 digits set it takes 60 s for the first code and 13 for the second one, with just the modifications explained before ... !

  12. #12
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Improve number combination vba

    Dear PCI,

    When you have time, if you could help me rebuild the code and make the macro go faster, I would be grateful.

    Regards,

    John

  13. #13
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Improve number combination vba

    Like any other program / application, excel will only use the memory that it needs at any given time. You should find that macros which use 11GB are carrying out more complex tasks.

    The only obvious way to make it use more memory is to make it less efficient. Then you have to decide if you want to use ~250MB for a macro that takes 1 minute, or ~10GB for a macro that takes 2 minutes?

  14. #14
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Improve number combination vba

    Is needed some details ...!
    You want to pick up p numbers out of n numbers.
    The total number of sets ( combinations ) is given by the formula:
    Fact(n) / Fact (p) / Fact(n-p)
    Formula crosschecked with you file
    in the file you sent n = 30 and p = 10 => Total = 30 045 015
    What is the max value for n and p, just to see amount of rows and if memory will be large enought

  15. #15
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Improve number combination vba

    Dear PCI,

    The max value for n is 50 and for p is 15, then Total = 2 250 829 575 120.

    "n" max value is 50, but is does not mean that the numbers are 1 to 50 (could be: 1, 2, 3, 5, 7, 8, 10, 12, 15, 20, [...] - but there will be a total of 50 numbers).

    As one "worksheet" will not support the total number of rows (2 250 829 575 120), the combinations may be saved in several files.

    Regards,

    John

  16. #16
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Improve number combination vba

    If that number is correct and you don't have a supercomputer in your living room the only thing left to do for you is to call NASA and ask them to calculate that for you.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  17. #17
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Improve number combination vba

    As one "worksheet" will not support the total number of rows (2 250 829 575 120), the combinations may be saved in several files.
    Clear, this explains some part of your code but then I have some dout about Excel and your computer's capacity to do the job

    Assuming total of rows = 2 250 829 575 120 and 1048576 rows per sheet this lead to 2146550 sheets ....!
    Last edited by PCI; 07-29-2018 at 04:37 AM.

  18. #18
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Improve number combination vba

    Dear Friends,

    Right now, and that's the reason why I have started this thread, I am concerned with a faster macro code for creating number combinations.

    I have always thought that using ARRAYS will bring an expressive speed boost. PCI has confirmed my inicial thought, but I don't know how to write a new code for creating number combinations using ARRAYS.

    But one thing I know for sure, that you guys here in this FORUM have a great knowledge and skills in writing macro codes, and are the ones that can help me rewrite this code.

    Your assistance will be very much appreciated.

    Regards,

    John
    Last edited by JOAO12; 07-30-2018 at 09:27 AM.

  19. #19
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Improve number combination vba

    using ARRAYS would bring an expressive speed boost
    Yes and your need is clear now,( it is not to increase the memory used ) but versus the memory size needed for some case I am not sure it is possible while using sheet you can add anther when you reach the botom of the previous one with memory you cannot ...!

  20. #20
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Improve number combination vba

    Dear PCI,

    Could you help me write a code for creating number combinations using ARRAYS ? The numbers that should be combined are placed in column "A" and the number of how many to be picked is in range "B1".

    Regards,

    John

  21. #21
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Improve number combination vba

    OKAY but for the first run I made I had memory issue, we will see what is the limits

  22. #22
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Improve number combination vba

    my 2 cents/input...

    First off, Excel is the wrong tool for this job. There are many reasons why this is the case.
    1. VBA is single threaded, thus very slow to do something like this.
    2. The results can easily surpass the available (and especially reasonable) space to store them available in Excel.
    3. There are tools designed to do this which do it faster and better.

    A scripting language/programming language like VBS, Java, C, etc are much better options if you want to make your own tool for this. They can be threaded, making better use of your CPU (and possibly your GPU) for parallel processing. The results are then typically dumped into a database like MySQL/SQL as they can be manipulated, viewed and stored in an efficient and usable way.

    Next, when it comes to VBA (or programming/computers in general) using more RAM never makes things faster. To elaborate, using more RAM than actually needed is a waste. In something like this the RAM usage should be pretty low, as its not memory intensive but CPU/GPU intensive.

    If you have macros using 11GB of RAM to process there are 2 basic possibilities as to why; A) you actually have that much data and it warrants the usage, B) regardless of the amount of data, your code's drastically inefficient. The first is extremely unlikely.

    When programming, the aim is generally using the least resources needed with the fastest outcome. You dont make your code faster by using more resources than the task reasonably needs, you make it faster by using less code and/or more efficient code.

    This loops back to my first point, VBA is single threaded. No matter how efficient you make the code, it can only handle 1 thread at a time. So as an example lets say your CPU can handle 8 threads at a time. No matter what you do in VBA you get 1 thread, where a language that can be threaded could use all 8 threads. That may not mean 8x faster in all cases, but generally speaking it would be significantly faster.
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  23. #23
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Improve number combination vba

    Don't want to let you hanging so I got into my archieves and found a code by Myrna Larson which does mainly what you are looking for.

    I've adapted it a bit to suit your requirements. The results are put on second sheet column by column. The only restriction is the number of cells on 1 sheet. (rows x columns)

    The combinations are comma-seperated.

    On Sheet1 column A are the numbers, combinations in B1 then press button.

    Tested it and it took under 2 minutes for over 3 million combinations.
    Attached Files Attached Files

  24. #24
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Improve number combination vba

    Dear Bakerman2,

    The code is very fast. I really appreciate your help.

    I just would like to ask you one more question: which changes should be done in the code to format the numbers so that the number combinatios are displayed as 01,02,03,04,05,06,07,08,09,10,11,12,13,14,15

    Make the numbers be displayed with 2 digits. For example: change "1" to "01"

    Thank you very much,

    John
    Last edited by JOAO12; 08-02-2018 at 12:31 PM.

  25. #25
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Improve number combination vba

    Format Column A as Text.

  26. #26
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: Improve number combination vba

    Dear friends,

    I would like to thank you all for your posts.

    Special thanks to Bakerman2 for helping me.

    Regards,

    John

  27. #27
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Improve number combination vba

    Glad to help and thanks for rep+. Much appreciated.

+ 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. Need help for creating a combination of number
    By factorjones in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-20-2017, 07:23 AM
  2. Combination without repetitions from 5 sets of number
    By alboholic in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-19-2016, 12:11 PM
  3. Text/number combination that I want to add
    By kjurss in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-03-2016, 10:21 AM
  4. Assign number to combination of checkboxes
    By laguna92651 in forum Excel General
    Replies: 3
    Last Post: 11-11-2014, 01:20 AM
  5. 6 Number Combination
    By shart2k12 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-13-2013, 07:20 PM
  6. Excel 2007 : different number combination
    By dh1 in forum Excel General
    Replies: 1
    Last Post: 04-21-2010, 12:11 AM
  7. number combination searching
    By labman in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 09-06-2007, 02:24 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