+ Reply to Thread
Results 1 to 16 of 16

Prevent excel from freezing while running for/next code

Hybrid View

  1. #1
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Prevent excel from freezing while running for/next code

    Hi guys,

    I've got a code that goes down a column with numbers, looks up a value on a website (through MSXML2.XMLHTTP method (thanks Kyle!)) and paste the result in a cell three columns next to the number.
    It's about 200 ~ 300 numbers (variable) and since every lookup takes about half a second, people get impatient if they have to wait.
    To "prevent" this, I do the following .

    For Each cell In Range("C7:C" & Range("C65536").End(xlUp).Row)
    If IsNumeric(cell.Value) = False Then GoTo nextart
    cell.Offset(0, 3).Activate
    cell.Offset(0, 3).Font.Bold = True
    Application.ScreenUpdating = False
    
    .... running webupdate code
    
    Application.ScreenUpdating = True
    cell.Offset(0, 3).Font.Bold = False
    nextart:
    Next cell
    Now this works fine, it activates the cell and bolds the text to show the user which cell is being updated.
    This however, only works for the first 10 rows it's doing. Then excel freezes and shows "NOT RESPONDING" in the top bar.
    This stays on screen until the whole column with numbers is done and then excel recomposes itself, having the last result activated and the macro done.

    How can I prevent excel from freezing at this point?
    Any suggestion would be more than welcome :-)
    Thanks so much!
    Please click the * below if this helps

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Prevent excel from freezing while running for/next code

    What about using something like:
    Application.StatusBar = X & "% Done"
    To keep the user updated? Where X represents the current progress as a percentage?

  3. #3
    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,730

    Re: Prevent excel from freezing while running for/next code

    You don't need to activate the cell to make it bold, so take the cell.offset.activate line out.

    Not sure it will help but it won't make it worse.


    Regards, TMS
    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


  4. #4
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Prevent excel from freezing while running for/next code

    yudlugar : I tried to do that, but doesn't help - freezes at same moment...

    TMS : I activate it so the screen will follow the cells after the row it goes "off screen", otherwise the cell will be made bold, but if it's off screen, the user won't see it
    Last edited by JasperD; 08-06-2013 at 04:12 AM.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Prevent excel from freezing while running for/next code

    Couldn't you do that with conditional formatting?

  6. #6
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Prevent excel from freezing while running for/next code

    Perhaps that would be possible - however, it's not about the highlight, it's about "how can I prevent excel from freezing while running code " :-)

  7. #7
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Prevent excel from freezing while running for/next code

    Does it still freeze and show not responding if you take out the stuff for making the cells activate/bold?

  8. #8
    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,730

    Re: Prevent excel from freezing while running for/next code

    Do you have any other code like Selection or Change event handlers?

    You could maybe hide all the columns to the right of column C at the beginning and unhide them at the end. Just trying to avoid unnecessary cell selection.

    Regards, TMS

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Prevent excel from freezing while running for/next code

    Have you tried turning screen updating on?

    I suspect that since you are running a web query a few hundred times, this will be the bottle neck and you don't update the screen while this is happening. You could also make the call async with a doEvents loop if it isn't already. Just bear in mind that all this will take even longer (and your're already doing things in the slowest possible way) and your users will still not be able to use their machines as clicking in the wrong place will royally screw things up.

  10. #10
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Prevent excel from freezing while running for/next code

    Maybe this, but I suspect it is the web call that is using the resources.
        Application.ScreenUpdating = False
        For Each Cell In Range("C7:C" & Range("C" & Rows.Count).End(xlUp).Row)
            If IsNumeric(Cell.Value) Then
                Cell.Offset(0, 3).Font.Bold = True
                '.... running webupdate code
            Else
                Cell.Offset(0, 3).Font.Bold = False
            End If
        Next Cell
        Application.ScreenUpdating = True
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  11. #11
    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,730

    Re: Prevent excel from freezing while running for/next code

    @Marcol: if you have the font bolding/unbolding inside the ScreenUpdating False/True, you're not going to see it.

    Regards, TMS

  12. #12
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Prevent excel from freezing while running for/next code

    @ TMS
    Why would you want to see it at every step ...
    Turn it red and bold if it needs to be clearer.

    See the attached
    Attached Files Attached Files

  13. #13
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Prevent excel from freezing while running for/next code

    Hi Marcol,

    I want to show it at every step so the user sees which row / value is being processed.
    But to be honest, it's not necessary to show anything, as long as EXCEL won't "freeze".
    Turning off screenupdating / events / calculation etc all doesn't do the trick, so I've just come to accept the fact that when running this extensive code, excel will just be hanging around for a couple of minutes.
    Probably due to the lame computers we have at work here...

  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,730

    Re: Prevent excel from freezing while running for/next code

    when running this extensive code
    Perhaps that's what you need to share to see if anyone ... Kyle, maybe ... can make it faster.

    Turning off screenupdating / events / calculation etc all doesn't do the trick
    Do you have event handlers?

    Regards, TMS

  15. #15
    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,730

    Re: Prevent excel from freezing while running for/next code

    @Marcol: First post ...

    it activates the cell and bolds the text to show the user which cell is being updated.

    Regards, TMS

  16. #16
    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,730

    Re: Prevent excel from freezing while running for/next code

    @Marcol: OT (sorry) ... your PM store is full.

+ 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. Prevent code from running on another open document
    By Marco-Kun in forum Word Programming / VBA / Macros
    Replies: 6
    Last Post: 12-20-2012, 01:39 AM
  2. excel freezing when i put in event code
    By Cicada in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-27-2012, 01:35 PM
  3. Vb code keeps freezing excel. Please help!
    By mkalenuik in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-16-2011, 04:52 PM
  4. [SOLVED] how to prevent code running when in a worksheet code
    By Corey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-13-2006, 04:00 AM
  5. Prevent Excel crash when running
    By rauxalacch in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-18-2005, 02:10 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