+ Reply to Thread
Results 1 to 12 of 12

My code is to slow for a large amount of numbers.

  1. #1
    Forum Contributor
    Join Date
    05-02-2013
    Location
    Indiana
    MS-Off Ver
    Excel 2016
    Posts
    190

    My code is to slow for a large amount of numbers.

    I wrote the following code to take two 3-digit numbers and check to see if there are any duplicate numbers among the six numbers. If there are no duplicates, I can continue with the code. If there are duplicate numbers, I ask the user to enter two different numbers. I start by asking user for two different numbers. Each of the two numbers must be 3-digits long. I convert the two numbers into a string and concatenate them to make a six digit number and then check the string for any duplicate numbers. I will be checking a large volume of numbers and my code runs too slow. Does any one else have a more concise way of coding this program? Thanks, redsab

    Please Login or Register  to view this content.

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: My code is to slow for a large amount of numbers.

    You only need one false flag:

    Please Login or Register  to view this content.
    Last edited by xladept; 03-14-2017 at 07:41 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: My code is to slow for a large amount of numbers.

    Hello redab,

    If you want to improve the speed you need to decrease the number of comparisons between the digits. Currently you are making 25 comparisons when it can be done in 7.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: My code is to slow for a large amount of numbers.

    Hi Redsab,

    Here is the site to read and learn about making your code faster.

    http://www.cpearson.com/excel/optimize.htm
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

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

    Re: My code is to slow for a large amount of numbers.

    1) When you declare variables, you need to specify the data type for each one in vba.
    Any variable without its data type will be recognized as Variant type.
    e.g
    Please Login or Register  to view this content.
    should be
    Please Login or Register  to view this content.
    2) This could be less loop...
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    05-02-2013
    Location
    Indiana
    MS-Off Ver
    Excel 2016
    Posts
    190

    Re: My code is to slow for a large amount of numbers.

    Leith, I reviewed your code and had a few questions and modified it slightly. 1) Did you change the integer value to long because the code can run long faster than an integer assignment? 2. Is it assumed that if a function is false unless you assign it to true? 3) I ran the code with the following values that should have been false but evaluated to true: {101,789}{789,101}{123,156}{123,416}{123,451}{123,245}{123,425}{123,452}. Thanks for your reply.

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    05-02-2013
    Location
    Indiana
    MS-Off Ver
    Excel 2016
    Posts
    190

    Re: My code is to slow for a large amount of numbers.

    xladept, thank you for your reply. I see how you cut out a lot of extra looping, that should help tremendously! Do you know if there is function that can take care of the comparing where you send it the string and it gives you a true value if there are duplicates? DUPLICATES(Num1, Num2) or something like that?
    Thanks redsab

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: My code is to slow for a large amount of numbers.

    Hello readsab,

    Question 1 - Did you change the integer value to long because the code can run long faster than an integer assignment?

    Answer 1 - No, it is just habit. There are few times when I code that i need a "short" integer (Integer Type). This limits your range from -32768 to 32767.


    Question 2 - 2. Is it assumed that if a function is false unless you assign it to true?

    Answer 2 - Yes, initially the function's value is False. only if no integers are the same will True be returned.


    Question 3 - I ran the code with the following values that should have been false but evaluated to true: {101,789}{789,101}{123,156}{123,416}{123,451}{123,245}{123,425}{123,452}

    Answer 3 - This is the expected output for the macro I wrote. The name of the function is "No Duplicate Numbers". If you want it to return False then the return value can be switched but you should also change the function name to "Duplicate Numbers".
    Last edited by Leith Ross; 03-15-2017 at 11:57 AM.

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: My code is to slow for a large amount of numbers.

    Hi RedSab,

    Were there such a function, I believe that Leith or Jindon would know of it - I know of no such function

  10. #10
    Forum Contributor
    Join Date
    05-02-2013
    Location
    Indiana
    MS-Off Ver
    Excel 2016
    Posts
    190

    Re: My code is to slow for a large amount of numbers.

    Leith, thank you for your help. I suppose I should call the function "NoDuplicateDigits". I didn't make my self fully clear of my output, sorry. What I should have said, was out of the six digits between 2-9 I can not have any of them the same. My ultimate project will print out a list of all the possible numbers that fit the following criteria: contain two 3-digit numbers where the sum of the two numbers are a 4-digit number and it uses all ten digits (0-9). Six of the digits will be the two numbers added together and the other four digits will be the answer. No duplications of digits in the summed numbers or the answer. I am just starting, so I have not programmed that far along yet. You have already taught me a few things. I appreciate it very much!
    Thanks, redsab
    Last edited by redsab; 03-15-2017 at 08:41 PM.

  11. #11
    Forum Contributor
    Join Date
    05-02-2013
    Location
    Indiana
    MS-Off Ver
    Excel 2016
    Posts
    190

    Re: My code is to slow for a large amount of numbers.

    MarvinP, Thank you I will check out that web site and learn some more! Thanks!
    redsab

  12. #12
    Forum Contributor
    Join Date
    05-02-2013
    Location
    Indiana
    MS-Off Ver
    Excel 2016
    Posts
    190

    Re: My code is to slow for a large amount of numbers.

    jindon, I did not know that the variables would be Variant type the way I had them listed. I will change my habit! I have tested your code and it works exactly the way I need it. Thanks. I was excited to see the line:
    Please Login or Register  to view this content.
    I have never used a formula quite like that before, but after I thought about it, it made sense. I am a little confused about the following code:
    Please Login or Register  to view this content.
    I am not sure what the conditional IF statement is doing, could you elaborate on it a little? What is the concatenation going on? Thanks, redsab
    Last edited by redsab; 03-15-2017 at 09:11 PM. Reason: To understand coding fully.

+ 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. adding a large, LARGE amount of text to a spreadsheet
    By Wolfpackfan in forum Excel General
    Replies: 4
    Last Post: 05-30-2016, 01:30 PM
  2. VBA pivot code doesn't work if the amount of source data becomes too large
    By punter in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-28-2014, 07:59 PM
  3. [SOLVED] Convert text into numbers when there is a large amount of data
    By iceordo in forum Excel General
    Replies: 4
    Last Post: 11-16-2012, 09:31 AM
  4. Help replacing large amount of text with another large amount of text??
    By floydrob in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-30-2012, 05:01 AM
  5. Replies: 1
    Last Post: 02-23-2011, 06:36 PM
  6. How do I see the amount in words (I will enter amount in numbers)
    By Ram in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-12-2006, 04:00 AM
  7. VBA Code to limit amount of text or numbers entered into a textbox?
    By funkymonkUK in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-19-2005, 07:39 AM

Tags for this Thread

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