+ Reply to Thread
Results 1 to 19 of 19

Find and delete range of data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-03-2009
    Location
    USA, California
    MS-Off Ver
    Excel 2007
    Posts
    385

    Find and delete range of data

    What I need is some code that will do the below code for a range of data with each new search searching the next cell, first pass the if statement checks O5 and then runs the search on N5 to delete the data. The second pass would then check O6 and run the search on N6. The range that would need to be searched and purged would be O2:O750. If anyone has any ideas on how to do this differently I'm open to suggestions, but at the very least if someone could help with what I ask even if it isn't the most effective.

    If Range("O5").Value = 1 Then
        Columns("A:J").Select
        Selection.Replace What:=Range("N5"), Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    End If
    Last edited by randell.graybill; 03-18-2010 at 08:40 PM.

  2. #2
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: Find and delete range of data

    Hi,

    You could try ...

    For each c in Range("O2:O750")
    If c.Value = 1 Then
    Columns("A:J").Replace What:=c.offset(0,-1), Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    End If
    Next c
    HTH

  3. #3
    Forum Contributor
    Join Date
    04-03-2009
    Location
    USA, California
    MS-Off Ver
    Excel 2007
    Posts
    385

    Re: Find and delete range of data

    Quote Originally Posted by JeanRage View Post
    Hi,

    You could try ...



    HTH
    I used JeanRages' code, as it was the only one that I tried that would work w/o being tweaked. The one with the special cells never found any data.

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Find and delete range of data

    Hi randell.graybill
    try some thing like....
    
    with worksheets("your sheet")
    eitem = myarray(.range("O5"), .range("N5"), .range("O6"), .range("n6"))
    for i = 0 To 2 step 2
    with columns("A:J")
    If eitem(i) = 1 then
    
    Replace What:=eitem(i + 1), Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    End If
    next i
    end with
    end with
    Last edited by pike; 03-06-2010 at 04:28 AM. Reason: add range("n6"))
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Find and delete range of data

    Try this
    Option Explicit
    
    Sub check()
    
        Dim rChk As Range
        Dim rDel As Range
        Dim rCl As Range
    
        Set rChk = Range(Cells(2, 15), Cells(750, 15)).SpecialCells(xlCellTypeConstants)
    
        For Each rCl In rChk
            If rCl.Value = 1 Then
                If rDel Is Nothing Then
                    Set rDel = rCl.Offset(0, -1)
                Else: Set rDel = Union(rDel, rCl.Offset(0, -1))
                End If
                rDel.Value = ""
            End If
        Next rCl
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    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: Find and delete range of data

    Not to sure of what you are trying to do, posting a sample workbook showing before and after would help.

    However you could try..........

    Code removed.

    Read the Question Alistair

    I must remember "Put the brain in gear before engaging the mouth"
    Last edited by Marcol; 03-06-2010 at 05:16 AM. Reason: I Read the Question!

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Find and delete range of data

    opps tested this one
    Option Explicit
    Sub pTest()
        Dim eitem, ikey As Byte
        With Worksheets("Sheet1")
            eitem = Array(.Range("O5"), .Range("N5"), .Range("O6"), .Range("N6"))
            For ikey = 0 To 2 Step 2
                With Columns("A:J")
                    If eitem(ikey) = 1 Then
                        .Replace What:=eitem(ikey + 1), Replacement:="", LookAt:=xlPart, _
                                 SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                                 ReplaceFormat:=False
                    End If
                End With
            Next ikey
        End With
    End Sub

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Find and delete range of data

    ok read the question
    maybe
    Option Explicit
    Sub pTest()
        Dim eitem, ikey As Long
        With Worksheets("Sheet1").Range("N5:O750")
            eitem = .Value
             For ikey = 1 To UBound(eitem, 2)
                With Columns("A:J")
                    If eitem(ikey, 2) = 1 Then
                        .Replace What:=eitem(ikey, 1), Replacement:="", LookAt:=xlPart, _
                                 SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                                 ReplaceFormat:=False
                    End If
                End With
            Next ikey
        End With
    End Sub

  9. #9
    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: Find and delete range of data

    Pike is correct, I should Read the Question!!!!

    Sub SearchAndReplace()
        Dim i As Integer
        For i = 2 To 750
            If Range("O" & i).Value = 1 Then
                Columns("A:J").Replace What:=Range("N" & i), Replacement:="", LookAt:=xlPart, _
                    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                    ReplaceFormat:=False
            End If
        Next
    End Sub
    I've Edited Post#5
    Cheers

  10. #10
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Find and delete range of data

    It will be interesting to see if there is any noticable speed difference between codes

  11. #11
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Find and delete range of data

    Quote Originally Posted by pike View Post
    It will be interesting to see if there is any noticable speed difference between codes
    I haven't tested speed,but in my code I use special cells to only select cells with values, then create one range to work with. I would think that would be faster

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Find and delete range of data

    Just created a workbook with dummy data in & run code to check speeds of mine & Pike's, here's the results

    check is Faster

    SearchAndReplace: 2.252065
    check: 1.980469
    Diff: 0.271596 Seconds

    Note I have added error handling to handle the possibility that nocells meet the criteria

    Sub check()
    
        Dim rChk As Range
        Dim rDel As Range
        Dim rCl As Range
        On Error GoTo exit_proc
        Set rChk = Range(Cells(2, 15), Cells(750, 15)).SpecialCells(xlCellTypeConstants)
    
        For Each rCl In rChk
            If rCl.Value = 1 Then
                If rDel Is Nothing Then
                    Set rDel = rCl.Offset(0, -1)
                Else: Set rDel = Union(rDel, rCl.Offset(0, -1))
                End If
                rDel.Value = ""
            End If
        Next rCl
        Exit Sub
    exit_proc:
        MsgBox "No cells found", vbCritical, "End"
    
    End Sub
    Last edited by royUK; 03-06-2010 at 07:14 AM. Reason: code changed to correct column

  13. #13
    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: Find and delete range of data

    How does this compare, or am I to late again?
    Sub SearchAndReplace()
        Dim i As Integer
        Dim strFirst As String
        Dim rng1 As Range, rng2 As Range
        
        Set rng1 = Range("O:O")
        Set rng2 = Range("A:J")
        
        rng1.Activate
        i = 0
        Do
            rng1.Find("1", ActiveCell, xlValues, xlWhole, xlByRows, xlNext, False, False).Activate
            If i > 0 And ActiveCell.Address = strFirst Then Exit Do
            If i = 0 Then strFirst = ActiveCell.Address
            rng2.Replace ActiveCell.Offset(0, -1), "", xlPart, xlByRows, False, False, False        
            i = i + 1
        Loop
        
        Set rng1 = Nothing
        Set rng2 = Nothing
    
    End Sub

  14. #14
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Find and delete range of data

    Hey Marcol
    never to late besides its a smart approach .. and must be quicker again

  15. #15
    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: Find and delete range of data

    Hi Pike

    Dinki-di , Cobber

    Slainte,
    Highlander

  16. #16
    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: Find and delete range of data

    Hi All

    SearchAndReplace (Post #12) v check

    SearchAndReplace is Faster

    SearchAndReplace: 0.1270 49
    check: 0.5652 99
    Diff: 0.4382 50 Seconds
    Checked with Aaron Bloods' Timetrial (add-in)

    http://www.xl-logic.com/modules.php?...download&cid=3

    Roy: Thank for pointing me to Aarons' add-in

    It will be very useful

    randell.graybill: Roys error handler would not go amiss added to SearchAndReplace

    Regards
    Alistair

    EDIT: Hold that result!
    I checked using dissimilar tables

    Roys' method is probably better.

    Apologies
    Alistair
    Last edited by Marcol; 03-06-2010 at 08:39 PM. Reason: Unfair test

  17. #17
    Forum Contributor
    Join Date
    04-03-2009
    Location
    USA, California
    MS-Off Ver
    Excel 2007
    Posts
    385

    Re: Find and delete range of data

    Wow thank you for the help from everyone, didn't know this would be so interesting for people. Anyway will test them and mark it solved when I decide which one I like best / works best and give rep to the person provided the code that I end up using.

  18. #18
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Find and delete range of data

    Hi randell.graybill
    Dont forget to change the thread prefix to solved

  19. #19
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Find and delete range of data

    I think we need clarification on exactly what is required. Are we searching A:J or
    The range that would need to be searched and purged would be O2:O750.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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