+ Reply to Thread
Results 1 to 30 of 30

Mistery: Inputbox makes my code 1000 times slower

  1. #1
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Mistery: Inputbox makes my code 1000 times slower

    Hello

    I have a macro that intends to copy the cells colors of selected range and place it somewhere else in the sheet

    The code takes less than 1/10 of a second, but if I make an Inputbox appear it will take now several seconds....

    This is my code:
    Please Login or Register  to view this content.

    STEPS:

    1) select a big coloured range to copy, for example NK5:UR10
    2) Run sub aaa from editor. It will copy the selected range to fixed cell MW22. It takes about 0.06 seconds to execute
    3) Uncomment red line above:
    Please Login or Register  to view this content.
    This makes an Inputbox appear and you need to click on any cell, nothing else is done.
    4) when you accept the inputbox, the code will take now several seconds



    Help will be much appreciated

    Many thanks!!

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,064

    Re: Mistery: Inputbox makes my code 1000 times slower

    Someone on another website had the same problem and said this

    "i solved it by adding Application.ScreenUpdating = true before inputbox then again i added Application.ScreenUpdating = False after it now its not slowing down"
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Mistery: Inputbox makes my code 1000 times slower

    This (potential) loop could be shortened and the user interface made more like Microsoft's.
    Please Login or Register  to view this content.
    by

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Mistery: Inputbox makes my code 1000 times slower

    And in this way ?
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Re: Mistery: Inputbox makes my code 1000 times slower

    Quote Originally Posted by Special-K View Post
    Someone on another website had the same problem and said this

    "i solved it by adding Application.ScreenUpdating = true before inputbox then again i added Application.ScreenUpdating = False after it now its not slowing down"
    It improves from 10 seconds to about 6 seconds.... but still is 6 seconds agains 0.06 seconds when Inputbox is not used

  6. #6
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Re: Mistery: Inputbox makes my code 1000 times slower

    Quote Originally Posted by mikerickson View Post
    This (potential) loop could be shortened and the user interface made more like Microsoft's.
    Please Login or Register  to view this content.
    by

    Please Login or Register  to view this content.
    I agree and thanks for the suggestion.... but this particular line has no effect in my problem, is there just to prove that making a SET without Inputbox does not affect the execution time

  7. #7
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Re: Mistery: Inputbox makes my code 1000 times slower

    Quote Originally Posted by porucha vevrku View Post
    And in this way ?
    Please Login or Register  to view this content.
    Thanks for the suggestion... it did not affect at all.... Wjat is the purpose of the "(1)" at the end??

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Mistery: Inputbox makes my code 1000 times slower

    When I run that code in 2010 after making the changes mentioned by Special-K, it is only slower due to the time taken to show the inputbox, select a cell and press OK. The actual processing doesn't appear to be slower.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  9. #9
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Mistery: Inputbox makes my code 1000 times slower

    As above xlnitwit
    Last edited by mjr veverka; 02-22-2018 at 12:54 PM.

  10. #10
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Mistery: Inputbox makes my code 1000 times slower

    As above xlnitwit
    Your order of events in the code:

    1. ini = Strings.Format(Now, "HH:nn:ss") & "." & Strings.Right(Strings.Format(Timer, "#0.00"), 2)
    2. Set desti = Application.InputBox(prompt:="Marca la primera cel·la de la nova ubicació", Type:=8)
    3. fini = Strings.Format(Now, "HH:nn:ss") & "." & Strings.Right(Strings.Format(Timer, "#0.00"), 2)
    4. MsgBox (ini & " ----> " & fini)

  11. #11
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Re: Mistery: Inputbox makes my code 1000 times slower

    Quote Originally Posted by xlnitwit View Post
    When I run that code in 2010 after making the changes mentioned by Special-K, it is only slower due to the time taken to show the inputbox, select a cell and press OK. The actual processing doesn't appear to be slower.
    I think you might be selecting too little cells....

    This is the excel file attached.

    Could you please try by selecting range NK5:UR10 before running the macro. After the inputbox is closed, it takes about 10 seconds in my Excel 2013
    Attached Files Attached Files

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

    Re: Mistery: Inputbox makes my code 1000 times slower

    Try declaring your variables (unless I missed it someplace). Especially variables you will set to objects. Dim desti2 as range.

    Declaring variables and using constants can have a very big impact on the speed of the code. A while back here I increased someones macro speed by ~33% simply by declaring their variables. If you do not declare them Excel has to evaluate type on each to ensure its valid as the code runs especially since variants can hold anything so it cannot be assumed that at line2 the variant is the same type as in line1.
    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

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

    Re: Mistery: Inputbox makes my code 1000 times slower

    For me the same speed with or without the input box.

    Be sure you change this:

    Please Login or Register  to view this content.
    when you uncomment the input box, otherwise its checking the old variable

  14. #14
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Re: Mistery: Inputbox makes my code 1000 times slower

    Quote Originally Posted by Zer0Cool View Post
    For me the same speed with or without the input box.

    Be sure you change this:

    Please Login or Register  to view this content.
    when you uncomment the input box, otherwise its checking the old variable
    Yes, thanks.... right now all I'm doing is pasting always in the same spot because I just wanted to know tha having a "SET" is not affecting

    I have declared the desti and desti 2 as Range.... same result: 0.06 seconds when line is commented and 10 seconds when inputbox appears.... I am very puzzled that you don't have this problem.... have you used my excel file and selected big range such NK5:UR10??

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

    Re: Mistery: Inputbox makes my code 1000 times slower

    Quote Originally Posted by munuelitu View Post
    have you used my excel file and selected big range such NK5:UR10??
    Ah this may be the base of the issue. Why would I select more than a single cell?

    Your code specifically says it can only be a single cell, so your static line:

    Please Login or Register  to view this content.
    is a single cell, and the above IF checks that it is only a single cell. If you had updated the IF like I had you shouldnt be able to select more than a single cell from the input box.

    Otherwise you have your answer why it takes longer....because there are more cells. Compare apples to apples. Make the static entry the same number of cells as your selection via input box.

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

    Re: Mistery: Inputbox makes my code 1000 times slower

    Also stepping back and looking at your code, I am not sure I understand the need for the complexity.

    Couldnt this be done without any arrays using 2 range objects and those properties since you are using selection or a continuous range of cells?

    IE (psuedo code):
    Please Login or Register  to view this content.
    I just dont see the need for filling 4 arrays, getting dimensions, multiple nested loops, etc. If you want to make it easy for the user, you can have them select the top leftmost cell of the range they want to dump into, and then resize it to the dimensions of the source range.

    range to range is much quicker than looping cells/ranges. Especially as the ranges get larger.

  17. #17
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Re: Mistery: Inputbox makes my code 1000 times slower

    HEllo ZeroCool... thanks so much for your suggestions

    I think I did not explain my self properly

    The main objective is to select a BIG range and copy it somewhere else, but ONLY cell fills and values, not borders. So the range to be copied is the selection (BIG) and the destination is indicated by a single cell (which will get the top-left cell of the big selection and copy the big range from there)

    I tried to do it with loops and Cell(row,column) statements and was veeeery slow.... then I tried arrays and the speed was amazing, just instant.... but then when I try to indicate the destination single cell with Inputbox I get a slow execution but JUST making it appear, even if I don't use the result of the Inputbox.....

    And this is what I want to figure it out

    Thanks a lot

  18. #18
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Re: Mistery: Inputbox makes my code 1000 times slower

    Quote Originally Posted by Zer0Cool View Post
    Also stepping back and looking at your code, I am not sure I understand the need for the complexity.

    Couldnt this be done without any arrays using 2 range objects and those properties since you are using selection or a continuous range of cells?

    IE (psuedo code):
    Please Login or Register  to view this content.
    I just dont see the need for filling 4 arrays, getting dimensions, multiple nested loops, etc. If you want to make it easy for the user, you can have them select the top leftmost cell of the range they want to dump into, and then resize it to the dimensions of the source range.

    range to range is much quicker than looping cells/ranges. Especially as the ranges get larger.
    The code does not work as expected... it makes a cell white for some reason....

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

    Re: Mistery: Inputbox makes my code 1000 times slower

    The code was psuedo code, IE not functional. It was only meant to convey the method.

    If possible Ill write something later to do this. The input box should have no impact on run speed what-so-ever except the time it takes to open, enter input in and hit ok. It doesnt seem any of us could duplicate the problem.

    If you could give a bullet point list of what the macro needs to do, that may help in understanding it (In other words not explaining your code as it is, but explaining what you want the macro to do)

  20. #20
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Mistery: Inputbox makes my code 1000 times slower

    Quote Originally Posted by munuelitu View Post
    ... but ONLY cell fills and values, not borders ...
    Copy all, then remove borders from cells, it will be faster.
    Or try maybe use FindFormat / ReplaceFormat (?)
    Last edited by mjr veverka; 02-22-2018 at 05:11 PM.

  21. #21
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Re: Mistery: Inputbox makes my code 1000 times slower

    Quote Originally Posted by Zer0Cool View Post
    The code was psuedo code, IE not functional. It was only meant to convey the method.

    If possible Ill write something later to do this. The input box should have no impact on run speed what-so-ever except the time it takes to open, enter input in and hit ok. It doesnt seem any of us could duplicate the problem.

    If you could give a bullet point list of what the macro needs to do, that may help in understanding it (In other words not explaining your code as it is, but explaining what you want the macro to do)
    I tried this:

    Please Login or Register  to view this content.
    An this is what happens: the grey source turns to black

    Using Ranges only.jpg

    The whole objective is as follows:

    Explanation.jpg

  22. #22
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Re: Mistery: Inputbox makes my code 1000 times slower

    Quote Originally Posted by Zer0Cool View Post
    The code was psuedo code, IE not functional. It was only meant to convey the method.

    If possible Ill write something later to do this. The input box should have no impact on run speed what-so-ever except the time it takes to open, enter input in and hit ok. It doesnt seem any of us could duplicate the problem.

    If you could give a bullet point list of what the macro needs to do, that may help in understanding it (In other words not explaining your code as it is, but explaining what you want the macro to do)
    I tried this:

    Please Login or Register  to view this content.
    Works good if colour of all cells in selection are the same, but if one cell color in selection is different than the others, the destination range turns black



    The explanation of what I want to achieve is in the next screenshot

    Attachment 562576
    Last edited by munuelitu; 02-22-2018 at 05:59 PM.

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

    Re: Mistery: Inputbox makes my code 1000 times slower

    Ok so it appears my logic may have a flaw for the fill colors. The property doesnt return an array of the colors. When using the .interior properties they are returning null or 0 when the range is multiple cells.

    Still looking to see if there is a way for the colors without the loop. The values certainly work as I showed however.

    EDIT: If the only thing you do NOT want to carry over formatting wise is borders, what I would do is copy the source range and for the destination range paste special formats, then set the destination range to not have borders.

    See attached

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Zer0Cool; 02-22-2018 at 06:57 PM.

  24. #24
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Re: Mistery: Inputbox makes my code 1000 times slower

    Good try Zer0Cool.... but as you can see in my screenshot above your post, my destination already has a different border layout... and the pastespecial will replace it with source layout and the xlNone will then erase, losing the original borders in the destination area....

    I've come to learn than when multiple cells need to be processed, arrays are the way to speed up the execution dramatically..... my problem is that the InputBox introduces an amazing delay for no apparent reason

    But if you can find a more direct way, be my guest

    Have you tried my code in my excel file with the Inputbox? How fast is it for a big range (like the one in my shot) using Inputbox?

  25. #25
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Mistery: Inputbox makes my code 1000 times slower

    I have tested your original code in 2010 with the Inputbox enabled and it is no slower than without it.

  26. #26
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Re: Mistery: Inputbox makes my code 1000 times slower

    Quote Originally Posted by xlnitwit View Post
    I have tested your original code in 2010 with the Inputbox enabled and it is no slower than without it.
    Oh Dear God... I just tested it in different computer with Excel 2003 and it is not slower either !!!

    Can anybody test with Excel 2013? This is the version I'm having problems with....

    I don't even know where to start looking now....

  27. #27
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Mistery: Inputbox makes my code 1000 times slower

    Is it 32bit or 64bit? I have 64bit somewhere at home.

  28. #28
    Registered User
    Join Date
    02-22-2018
    Location
    Barcelona
    MS-Off Ver
    2007
    Posts
    18

    Re: Mistery: Inputbox makes my code 1000 times slower

    It's 64 bits...

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

    Re: Mistery: Inputbox makes my code 1000 times slower

    I tested in 2013, no difference for me with/without input box.

    I have an idea, in your screenshot the source and destination both have borders, just different ones right?

    Why not just set the destinations borders to the desired type?

    Ill post an example

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

    Re: Mistery: Inputbox makes my code 1000 times slower

    Very minor change to the code:

    Please Login or Register  to view this content.
    So basically...we still copy all the formats, including border and then just change it to match your desired borders.

+ 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 2013 VBA - Operning CSV file in loop becomes slower and slower
    By maruthu22 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-17-2017, 12:36 AM
  2. Excel Spreadsheet running VBA macros gets slower and slower over time
    By AliJay in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-20-2017, 01:31 AM
  3. What causes a macro to run slower at certain times?
    By Wegener in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-03-2014, 02:20 PM
  4. [SOLVED] HELP... Commission of 20% if an employee makes more than 2.7 times their wages
    By pogo.stix in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-08-2012, 09:23 AM
  5. Editing makes file slower
    By footloose in forum Excel General
    Replies: 4
    Last Post: 04-05-2012, 01:11 AM
  6. VBA routine gets slower and slower on each iteration of the main loop
    By whitespaces in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-15-2009, 03:29 AM
  7. [SOLVED] [SOLVED] Pls confirm 2007 chart redraw is up to 10 times slower than 2003
    By larry godfrey in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 07-06-2006, 09:55 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