Results 1 to 8 of 8

Thread: [RESOLVED] Lookup deletion rows problems excel crashing

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Resolved [RESOLVED] Lookup deletion rows problems excel crashing

    I have some code below which is crashing.

    I need to delete items in sheet 1 not shown in sheet 2 of a lareg spreasheet 35000 plus.

    Can someone clean up my code so stops crashing and ist quicker. It seems to work for small number of rows, but not for large number of rows.


    There must be something simple wrong. An explannation of the code would be great as well.

    VB Code:
    1. Sub SelectedDesigns2()
    2.  
    3. Dim sheetcount As Integer
    4. Dim FileLength As Long
    5. Dim sheetname As String
    6.  
    7. sheetcount = Worksheets.Count
    8.  
    9. If sheetcount < 2 Then
    10. MsgBox ("Muppet! You do not have a selected designs sheet.")
    11. Else
    12.  
    13. 'Activate sheet
    14. Worksheets(2).Activate
    15.  
    16. sheetname = ActiveSheet.Name
    17.  
    18. 'Activate sheet
    19. Worksheets(1).Activate
    20.  
    21. 'Find how many codes there are
    22. FileLength = ActiveSheet.UsedRange.Rows.Count
    23.  
    24.     Range("B:B").Select
    25.     Application.CutCopyMode = False
    26.     Selection.Insert Shift:=xlToRight
    27.     Range("B2").Select
    28.     ActiveCell.FormulaR1C1 = _
    29.     "=IF(ISERROR(VLOOKUP(RC[-1],'" & sheetname & "'!C1:C1,1,FALSE)),""n"",""y"")"
    30.    
    31.     Selection.AutoFill Destination:=Range("B2:B" & FileLength)
    32. End If
    33.  
    34. Range("B:B").Select
    35. Selection.Cells.Copy
    36. Range("b2").PasteSpecial xlPasteValues
    37.  
    38.  
    39. Range("B2").Activate
    40.  
    41.  
    42.  
    43. For i = 2 To FileLength
    44. If ActiveCell.Text = "y" Then
    45. ActiveCell.Offset(1, 0).Activate
    46. Else
    47. ActiveCell.EntireRow.Delete
    48. End If
    49. Next i
    50.  
    51. Range("B:B").Delete
    52.  
    53.  
    54. End Sub

    Boris

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Lookup deletion rows problems excel crashing

    Boris
    For Deletes, you are always better off starting at the bottom of the range and working upwards.
    Replace your code
    VB Code:
    1. Range("B2").Activate
    2.  
    3. For i = 2 To FileLength
    4. If ActiveCell.Text = "y" Then
    5. ActiveCell.Offset(1, 0).Activate
    6. Else
    7. ActiveCell.EntireRow.Delete
    8. End If
    9. Next i
    with
    VB Code:
    1. Range("B1").Activate
    2.  
    3. For i = FileLength - 1 To 1 Step -1
    4.     With ActiveCell.Offset(i, 0)
    5.         If .Text = "y" Then
    6.             .EntireRow.Delete
    7.         End If
    8.     End With
    9. Next i

    and you should be good to go.

    One other thing to note: try to avoid using the .Activate method of a range wherevere possible - it really slows down your code, especially in loops.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Re: Lookup deletion rows problems excel crashing

    Hey kenny,

    Its still not working properly. At all really , just run it now for long spreadsheet for 1/2 hour with no sucess. Is some of the other code wrong or badly written, I think it might be do with forumla in cell when trying to delte rows, I am trying to paste values, to avoid this but no luck. Don't think its even pasting values properly. I have hoping a 35,000 spreadsheet would take no more than 5 minutes to finish.

    I would apprecaite if you could help debug.


    VB Code:
    1. Sub SelectedDesigns2()
    2.  
    3. Dim sheetcount As Integer
    4. Dim FileLength As Long
    5. Dim sheetname As String
    6.  
    7. sheetcount = Worksheets.Count
    8.  
    9. If sheetcount < 2 Then
    10. MsgBox ("Muppet! You do not have a selected designs sheet.")
    11. Else
    12.  
    13. 'Activate sheet
    14. Worksheets(2).Activate
    15.  
    16. sheetname = ActiveSheet.Name
    17.  
    18. 'Activate sheet
    19. Worksheets(1).Activate
    20.  
    21. 'Find how many codes there are
    22. FileLength = ActiveSheet.UsedRange.Rows.Count
    23.  
    24.     Range("B:B").Select
    25.     Application.CutCopyMode = False
    26.     Selection.Insert Shift:=xlToRight
    27.     Range("B2").Select
    28.     ActiveCell.FormulaR1C1 = _
    29.     "=IF(ISERROR(VLOOKUP(RC[-1],'" & sheetname & "'!C1:C1,1,FALSE)),""n"",""y"")"
    30.    
    31.     Selection.AutoFill Destination:=Range("B2:B" & FileLength)
    32. End If
    33.  
    34. Range("B:B").Select
    35. Selection.Cells.Copy
    36. Range("b2").PasteSpecial xlPasteValues
    37.  
    38.  
    39. Range("B2").Activate
    40.  
    41.  
    42.  
    43. For i = 2 To FileLength
    44. If ActiveCell.Text = "y" Then
    45. ActiveCell.Offset(1, 0).Activate
    46. Else
    47. ActiveCell.EntireRow.Delete
    48. End If
    49. Next i
    50.  
    51. Range("B:B").Delete
    52.  
    53.  
    54. End Sub

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Lookup deletion rows problems excel crashing

    It looks like you haven't made the changes I suggested.

    Also can you upload a sample file?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Re: Lookup deletion rows problems excel crashing

    For some reason, I can't upload .xls files. I tried the code as below- no luck kenny. All I am doing is deleting theitems on sheet 1 that are no on the lookup table sheet 2.

    Appreciate Borris.
    Sub SelectedDesigns2()

    Dim sheetcount As Integer
    Dim FileLength As Long
    Dim sheetname As String

    sheetcount = Worksheets.Count

    If sheetcount < 2 Then
    MsgBox ("Muppet! You do not have a selected designs sheet.")
    Else

    'Activate sheet
    Worksheets(2).Activate

    sheetname = ActiveSheet.Name

    'Activate sheet
    Worksheets(1).Activate

    'Find how many codes there are
    FileLength = ActiveSheet.UsedRange.Rows.Count

    Range("B:B").Select
    Application.CutCopyMode = False
    Selection.Insert Shift:=xlToRight
    Range("B2").Select
    ActiveCell.FormulaR1C1 = _
    "=IF(ISERROR(VLOOKUP(RC[-1],'" & sheetname & "'!C1:C1,1,FALSE)),""n"",""y"")"

    Selection.AutoFill Destination:=Range("B2:B" & FileLength)
    End If

    Range("B:B").Select
    Selection.Cells.Copy
    Range("b:b").PasteSpecial xlPasteValues


    Range("B2").Activate



    For i = FileLength - 1 To 1 Step -1
    With ActiveCell.Offset(i, 0)
    If .Text = "y" Then
    .EntireRow.Delete
    End If
    End With
    Next i

    Range("B:B").Delete


    End Sub

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Re: Lookup deletion rows problems excel crashing

    Here a winzip file - cheers.

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Lookup deletion rows problems excel crashing

    Boris
    Based on you test.xls file, I wrote the following code. When I ran it against the test data, it ran in just under 13 minutes.
    Per my test, all rows without matches were deleted and none with matches were deleted. (no false Positives or Negatives).

    Can you run this version of the code and let me know if it meets your needs.

    The main performance boosts in my version of the code are from;
    1/ Not using the .Select method
    2/ Not using the .Activate method
    3/ Not inserting any formulas. (The recalc associated with this was probably your biggest performance hit)
    4/ Turning off App level properties that slow performance.
    VB Code:
    1. Sub BorisDelete()
    2.  
    3. Dim lRowCount As Long
    4. Dim rngData As Range
    5. Dim rngLookup As Range
    6. Dim oCurrentCalc
    7. Dim lRowNum As Long
    8. Dim vLookupValue As Variant
    9.    
    10.     'This isn't strictly necesary,
    11.     'but I love your Msg!
    12.     If ThisWorkbook.Worksheets.Count < 2 Then
    13.         MsgBox ("Muppet! You do not have a selected designs sheet.")
    14.         Exit Sub
    15.     End If
    16.    
    17.     'Set a reference to the range
    18.     'of know good values
    19.     With Worksheets(2)
    20.         lRowCount = .UsedRange.Rows.Count
    21.    
    22.         'Note: I started at row 1 as there are no headers
    23.         Set rngLookup = .Range(.Cells(1, 1), .Cells(lRowCount, 1))
    24.     End With
    25.    
    26.     'Set a reference to the range
    27.     'of values to test
    28.     With Worksheets(1)
    29.         lRowCount = .UsedRange.Rows.Count
    30.        
    31.         'Note: I started at row 2 to exclude the headers
    32.         Set rngData = .Range(.Cells(2, 1), .Cells(lRowCount, 1))
    33.     End With
    34.    
    35.     'Turn off App level Properties
    36.     'to improve performance
    37.     With Application
    38.         oCurrentCalc = .Calculation
    39.         .Calculation = xlCalculationManual
    40.         .ScreenUpdating = False
    41.     End With
    42.    
    43.     With rngData
    44.        
    45.         'Loop through all rows in reverse
    46.         For lRowNum = .Rows.Count To 1 Step -1
    47.            
    48.             'Get the value from the current row
    49.             vLookupValue = .Cells(lRowNum, 1).Value
    50.            
    51.             'Look for that value in the range of known good values
    52.             'Using the .Find Method of the Range Object
    53.             'Checking for a complete match (xlWhole)
    54.             If rngLookup.Find(What:=vLookupValue, LookIn:=xlValues, LookAt:=xlWhole) Is Nothing Then
    55.                
    56.                 'If no match - delete the entire row
    57.                 .Rows(lRowNum).EntireRow.Delete Shift:=xlShiftUp
    58.             End If
    59.            
    60.         Next lRowNum
    61.     End With
    62.    
    63.     'Reset App level Properties
    64.     With Application
    65.         .Calculation = oCurrentCalc
    66.         .ScreenUpdating = True
    67.     End With
    68.    
    69.     'Clear Object Variables
    70.     Set rngLookup = Nothing
    71.     Set rngData = Nothing
    72. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Re: Lookup deletion rows problems excel crashing

    Nice one kenny!Your a true legend.

    I doubt very much it can speeded up more, I supsect this is a quick as it gets. Am I right?

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