Consulting

Results 1 to 4 of 4

Thread: Why Is my code so painfully slow?

  1. #1

    Why Is my code so painfully slow?

    Is the '.Clear' method what is slowing me down? I need to clear a cell if it contains a "c"

    If it contains a "c" it also has blue format from some conditional formatting that the workbook came to me with.

    I know that there are quite a few cells for it to check (108,000) but I know Excel can do better than this. It's taking minutes, and even then, my PC stops responding.

    Attached worksheet.

    Option Explicit
    Dim i As Long
    Dim j As Long
    Dim nRow As Long
    Dim nCol As Long
    
    
    Sub DeleteCoolers()
    nRow = 2000
    nCol = 56
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    For i = 1 To nRow
        For j = 1 To nCol
             'Already checked 1st and Last 2 slots of each cluster manually-->no coolers in those spots
            If Cells(i + 3, j + 2) = "c" Then
                Cells(i + 3, j + 2).Clear
            End If
        Next j
    Next i
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    End Sub
    EDIT: can't upload....making me wonder why this file is so big....

  2. #2
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location
    Perhaps replace is quicker? used macro recorder to get the code...
    Option Explicit
    
    Sub DeleteCoolers()
    Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False
    Range(Cells(1, 1), Cells(2000, 56)).Replace What:="c", Replacement:="", LookAt:=xlPart, SearchOrder:= _
            xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    End Sub

  3. #3
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    Make sure you change the
    LookAt:=xlPart to LookAt:=xlWhole
    otherwise all of your C's will be gone. You'll have oolers instead of coolers

  4. #4
    Thanks for the suggestions. I just removed the conditional formatting and then used the .Clear method instead of .ClearContents. Much faster!

Posting Permissions

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