Results 1 to 5 of 5

Thread: Excel freezes when running code

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    1

    Excel freezes when running code

    I have written code for a macro, the first part of which looks at the whole worksheet (which has over 60 000 entries), prompts the user to enter a specific country name and then copies all the entries for that country in question into a separate worksheet. Here is the relevant part of that code:

    Country = InputBox("Enter a country name")
    NoClients = Application.WorksheetFunction.CountIf(Range("A:A"), "<>") - 1

    For i = NoClients To 0 Step -1
    If Cells(i + RowOffset, ColumnOffset) = Country Then
    Rows(i + RowOffset).EntireRow.Copy

    Worksheets("Country").Activate
    Excel.Range("A2").Insert Shift:=xlDown
    Worksheets("Sheet1").Activate

    End If
    Next i

    Now the code works fine, except for when I enter Japan or USA, Excel freezes. Both have the highest number of entries in my spreadsheet. China has about 4,500 it works fine, USA has 6,500 and japan even more. I am turning screen updating off and formula calculation to manual. I really am not sure why this is occurring, am I trying to push my PC beyond its capabilities, or is there something wrong with the code?

    Many thanks to anyone who can offer any advice.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Excel freezes when running code

    does it ever come back??
    or when it freezes.. is it "done"
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel freezes when running code

    if you address each sheet, instead of activating them when you copy, it will probably cut down the time

    VB Code:
    1. Country = InputBox("Enter a country name")
    2. NoClients = Application.WorksheetFunction.CountIf(Range("A:A"), "<>") - 1
    3.  
    4. For i = NoClients To 0 Step -1
    5. If Cells(i + RowOffset, ColumnOffset) = Country Then
    6. Rows(i + RowOffset).EntireRow.Copy
    7.  
    8. 'Worksheets("Country").Activate
    9. sheets("Country").Range("A2").Insert Shift:=xlDown
    10. 'Worksheets("Sheet1").Activate
    11.  
    12. End If
    13. Next i

    don't know that will help with your problem though

    pete

  4. #4
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Excel freezes when running code

    ...and put a DoEvents in your big loop.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel freezes when running code

    Also do a ScreenUpdating to help free cpu cycles.
    VB Code:
    1. 'Turn updating of screen off
    2. Application.ScreenUpdating = False
    3. 'Do your loop
    4. 'Finish loop
    5. 'Turn updating back on
    6. Application.ScreenUpdating = True
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width