+ Reply to Thread
Results 1 to 26 of 26

Step within Macro is taking forever to complete and locks up excel until complete

  1. #1
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Step within Macro is taking forever to complete and locks up excel until complete

    I have a need within my macro to automate a process of taking multiple columns worth of numbers and formatting them to be used outside of excel in a SQL query
    So i need to take a list of numbers and format like so.
    Original = 012945867
    Expected = '012945867',

    So my question is 2 parts.

    #1 = How can this be setup to automatically get the data without user intervention, currently as you will see, when they get to this step, they are prompted to complete the data, BUT, Ive already setup to be pre-populated, but that still involves the user clicking thru the prompts.. So how can i do away with the prompts and just pass in the data

    #2 = Once the above is accomplished, how can i make this process faster/ more efficient? Like i said, when this process runs, it takes minutes to run and this weeks test was only 212 numbers that it needed to format.

    Any and all help/suggestions would be greatly appreciated.
    Last edited by cubangt; 05-15-2017 at 02:35 PM.

  2. #2
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    would you care to show your code? Also how this query is executed - from excel?. what's the SQL statement
    with 212 numbers it's not the #1 of the macro, I would say it;s waiting for the the sql query (or queries if you run let's say 212 queries).
    If you are pleased with a member's answer then use the Star icon to rate it.

  3. #3
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Guess it would help if i posted the code in question, sorry about that..

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Can you post the current code?
    If posting code please use code tags, see here.

  6. #6
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    The sql is not part of it, we are merely taking a list of numbers and formatting it so that it can be executed in SQL(manual step)
    basically lists in columns like so
    Please Login or Register  to view this content.
    Last edited by cubangt; 05-15-2017 at 02:36 PM.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,427

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Maybe:

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Which step in the posted code is slowing things up?

  9. #9
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Im working thru this now, reason for posting now, since i cant seem to find what is causing the issue..
    BUT now that im stepping thru, i believe its not the above code, but these 2 routines

    Please Login or Register  to view this content.

  10. #10
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Quote Originally Posted by TMS View Post
    Maybe:

    Please Login or Register  to view this content.
    So i replaced my logic with the suggested above and it did improve the speed a bit, but still locking up excel and processing taking long time..

    Here is what i did within my routine that does the text formatting.
    Please Login or Register  to view this content.
    Since the processing did speed up, im now thinking this is what is taking longer to complete.. I know that using "Selection" is not the most optimal way, so what can i do to improve this logic?

    Please Login or Register  to view this content.

  11. #11
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Something i just noticed with the code provided and im testing, is that its only looking at column A, but there is a chance that there could be couple thousand numbers to format, is there not a way to do the same but with only cells that have data?

    initial tests when we created this macro, was 5000 records, split into 5 columns with 999 records per column. So in that example i would need to insure that all 5000 records are formatted correctly.. not just column A
    That was the reason for using these lines in my original code

    Please Login or Register  to view this content.

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,427

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    I have a need within my macro to automate a process of taking a column worth of numbers and formatting them
    You get what you ask for ...

  13. #13
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    yea i realized that now.. already updated all mentions of column to columns..
    The "Split" routine can output 1 column or multiple columns, just depends on the list of values it receives to split out.

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,427

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Your AppendComma routine is reading and writing individual cells in the selection. Switching off screen updating would make that much quicker. That said, there doesn't seem much point having separate routines to format the non blank cells as text and then add a comma. Those routines could be combined.

    My code reads all the data in column A into an array, processes it, and writes it back ... no interaction with the Worksheet. That code could easily be extended to process multiple columns AND add the comma (I would have thought.

    Post a sample workbook with some typical data and before and after sheets.

  15. #15
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Attached is a stripped down version of the file, if you go to the "processing" there is the button that calls the routines, the output ends up on the "SQL Statement" tab
    To cut down on file size, i went ahead and provided the text in the other 2 tabs "Raw Report Data" and "Final Report" those are sources used within the routines

    Really appreciate the assistance.. everyone is happy with how it works, its just slow for that portion.. every other process runs within seconds.. i mean 2 or 3 seconds

    thank you
    Attached Files Attached Files

  16. #16
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    So that functionality can be retested over and over, all you have to do is delete the data from the "SQL statement" tab and click the button again.. just in case, the other tabs need the data in order to have something to split.

  17. #17
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    So i just fixed the delay..
    I removed 2 lines from both routines and clicking the button instantly returned my list all formatted

    Please Login or Register  to view this content.
    I got rid of these 2 lines
    'Range(Selection, Selection.End(xlDown)).Select
    'Range(Selection, Selection.End(xlToRight)).Select

    Since im already using this line to get the region with data
    Range(wSheetCell).CurrentRegion.Select

    Please let me know if there is anything else i can do to make it better.

  18. #18
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,427

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Please Login or Register  to view this content.


    Please Login or Register  to view this content.
    So, that's processing 210 x 16,384 cells = 3,440,640 cells


    Whatever, try this way:

    Please Login or Register  to view this content.

  19. #19
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    thanks, i will try tomorrow when i get back to work. So the for each loop is quicker using range than selection? I mean i keep reading that selection is the wrong way to do certain tasks, but ok for others..

  20. #20
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    The problem with the code is in

    Please Login or Register  to view this content.
    and the range you are working with. So, you actually loop trough 210 columns or close to 3.5 million cells
    Last edited by buran; 05-16-2017 at 04:16 AM.

  21. #21
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Quote Originally Posted by cubangt View Post
    thanks, i will try tomorrow when i get back to work. So the for each loop is quicker using range than selection? I mean i keep reading that selection is the wrong way to do certain tasks, but ok for others..
    There is almost no use case that will require selecting things (like you do), but working with Selection maybe OK (e.g. if you want to allow user to select cells to process).
    In this case however you end up selecting full 210 columns and start working with these columns

  22. #22
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    I changed the routine yesterday to this and everything processes within a second or two.

    Please Login or Register  to view this content.
    So even eliminating these 2 lines its still not efficient?
    Please Login or Register  to view this content.
    The purpose of this macro is to speed things up for the user, and with little user intervention.
    The user is only suppose to paste external data into 2 different sheets, and then the macro(via buttons) will take all that data and create a report that is sent out to other users..

    Reason for this template was because the lady that does this would take little over an hour to do, maybe less, so instead of all these manual steps to apply formatting, vlookups, and manually formatting to copy into SQL, figured i would speed the process up for her.

    Yesterdays afternoon test with the above changes proved promising, copied in the raw data, macro ran within 2 seconds, copied results from SQl and remaining 2 buttons took about the same time.. 2 seconds..

    so if all works well, the longest part of the entire process will be when she has to login to SQL and run the query with the formatted list. So i think if she has everything open and ready to go, the process will take no longer than 5 minutes

  23. #23
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    Anyway, I would not select, but work with the range directly
    And next step could be to use ADO to access the database directly from excel, so no need for manual intervention at all

  24. #24
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,427

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    As Buran says, you should avoid selection in almost every case. In my solution, the first routine loads the data into an array, processes it, and writes it back to the sheet. The second routine does amend the cells on the sheet, however, it does not select the sheet or any of the cells.

    Switching screen updating off before processing and on again afterwards will often improve performance where you are selecting cells.


    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  25. #25
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    TMS, i went ahead and updated my earlier logic to your suggestion and works great.. I also added to your reputation and marked the thread solved..

    THANK YOU VERY MUCH!!

  26. #26
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,427

    Re: Step within Macro is taking forever to complete and locks up excel until complete

    You're welcome. Thanks for the rep.

+ 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. [SOLVED] Macro is taking very long to complete
    By kevivu in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-02-2015, 03:53 AM
  2. AND Function - Change T/F to Complete/ Not Complete
    By PK5555 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-08-2014, 02:27 AM
  3. [SOLVED] Copy taking an inordinate time to complete
    By jacob@thepenpoint in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-22-2014, 02:56 PM
  4. Macro Taking Forever to Complete
    By hk4kim in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 07-22-2013, 01:40 PM
  5. Excel Macro does not complete with shortcut key.
    By CJPHX in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-28-2010, 09:35 AM
  6. Single Step InPut Box Locks Up Excel
    By FredOakden in forum Excel General
    Replies: 1
    Last Post: 06-02-2005, 05:05 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