Consulting

Results 1 to 5 of 5

Thread: Solved: faster way to delete row

  1. #1
    VBAX Tutor
    Joined
    Sep 2007
    Posts
    265
    Location

    Solved: faster way to delete row

    HI there

    I have around 60.000 of rows data.
    I want to delete row if there is a certain value in multiple column.

    Please find the following for reference.

    Thanks in advance
    [vba]
    Sub DEL_Code()
    Dim i As Long, lastrow As Long
    Dim Start As Double, Finish As Double

    Start = Timer

    With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    End With

    With ActiveSheet
    lastrow = Cells.SpecialCells(xlCellTypeLastCell).Row
    End With


    For i = lastrow To 1 Step -1

    If Cells(i, "L").Value <> "00000000" Then
    Cells(i, "L").EntireRow.Delete

    If Cells(i, "Q").Value <> "00000000" Then
    Cells(i, "Q").EntireRow.Delete

    If Cells(i, "O").Value <> "1" Or _
    Cells(i, "O").Value <> "2" Then
    Cells(i, "O").EntireRow.Delete

    If UCase(Cells(i, "M").Value) Like "W*" Or _
    Cells(i, "M").Value Like "D*" Or _
    Cells(i, "M").Value Like "E*" Or _
    Cells(i, "M").Value Like "Q*" Or _
    Cells(i, "M").Value = "O3" Or _
    Cells(i, "M").Value = "O4" Then
    Cells(i, "M").EntireRow.Delete

    If UCase(Cells(i, "R").Value) Like "W*" Or _
    Cells(i, "R").Value Like "D*" Or _
    Cells(i, "R").Value Like "E*" Or _
    Cells(i, "R").Value Like "Q*" Or _
    Cells(i, "R").Value = "O3" Or _
    Cells(i, "R").Value = "O4" Then
    Cells(i, "R").EntireRow.Delete

    End If
    Next i



    With Application
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With

    Finish = Timer
    MsgBox "Delete Time: " & Finish - Start & " Second"

    End Sub
    [/vba]

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    How much of the data is being deleted? More than 50%? Would it be faster to write to data you want to KEEP to a new sheet, then delete/rename the raw data?

    David


  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,455
    Location
    [vba]

    Sub DEL_Code()
    Const FORMULA_TEST As String = _
    "=OR(L2<>""00000000"",Q2<>""00000000""," & _
    "OR(LEFT(M2,1)={""W"",""D"",""E"",""Q""}),OR(M2={""O3"",""O4""})," & _
    "OR(LEFT(R2,1)={""W"",""D"",""E"",""Q""}),OR(R2={""O3"",""O4""}))"
    Dim i As Long, lastrow As Long
    Dim rng As Range
    Dim Start As Double, Finish As Double

    Start = Timer

    With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    End With

    With ActiveSheet

    lastrow = Cells.SpecialCells(xlCellTypeLastCell).Row

    .Columns(20).Insert
    .Rows(1).Insert
    .Range("T1") = "temp"
    .Range("T2").Resize(lastrow).Formula = FORMULA_TEST

    Set rng = .Range("T1").Resize(lastrow + 1)
    rng.AutoFilter Field:=1, Criteria1:="=TRUE"
    On Error Resume Next
    Set rng = rng.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If Not rng Is Nothing Then rng.Delete

    .Columns(20).Delete
    End With


    With Application
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With

    Finish = Timer
    MsgBox "Delete Time: " & Finish - Start & " Second"

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Tutor
    Joined
    Sep 2007
    Posts
    265
    Location
    Hi bob
    you did Great trick. it save 2 minutes from 5 minutes.

    as always Thanks for assistance

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post a small sample of your data?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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