+ Reply to Thread
Results 1 to 12 of 12

macro - search and delete

Hybrid View

  1. #1
    Registered User
    Join Date
    01-26-2012
    Location
    dublin
    MS-Off Ver
    Excel 2007
    Posts
    6

    macro - search and delete

    Hi,
    I was trying to do a macro to find a value in a column and delete it from every row.

    This is my code:

    Dim FoundCell As Range
    Application.ScreenUpdating = False
    Set FoundCell = Range("C:C").Find(what:="Mobile")
    Do Until FoundCell Is Nothing
    FoundCell.EntireRow.Delete
    Set FoundCell = Range("C:C").FindNext
    Loop

    In this way I delete all the rows where in the column C there is the word "Mobile".

    It's working properly... the problem is that I have many different words to look for and for the moment I am just adding and readding lines of code.
    There is a better way???

    Now my code is like that:

    Dim FoundCell As Range
    Application.ScreenUpdating = False
    Set FoundCell = Range("C:C").Find(what:="Mobile Device")
    Do Until FoundCell Is Nothing
    FoundCell.EntireRow.Delete
    Set FoundCell = Range("C:C").FindNext
    Loop

    Set FoundCell = Range("C:C").Find(what:="Supply")
    Do Until FoundCell Is Nothing
    FoundCell.EntireRow.Delete
    Set FoundCell = Range("C:C").FindNext
    Loop

    Set FoundCell = Range("C:C").Find(what:="Blackberry")
    Do Until FoundCell Is Nothing
    FoundCell.EntireRow.Delete
    Set FoundCell = Range("C:C").FindNext
    Loop


    The problem is that I can have 20 or 30 words...
    Any help will be appreciated.
    Thanks
    Jsabina

  2. #2
    Registered User
    Join Date
    01-26-2012
    Location
    dublin
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: macro - search and delete

    A quick tip?
    If I need to use another kind of function and not Find
    I am looking around but I cannot find a solution.
    I am beginner..

    thanks!!

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: macro - search and delete

    Welcome to the forum.

    It would be good if you post your code within code tags in the future.

    You can list the words in one column of your macro file and then use a loop to go through each word and perform the necessary actions. This way, you wouldnt need to code it again and again.

    Let me know if you need more help with this.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  4. #4
    Registered User
    Join Date
    01-26-2012
    Location
    dublin
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: macro - search and delete

    Hi Arlu,
    thanks and sorry if I posted without the code tags.
    So do you mean using always the find?

    But using some if statements can I do the same?
    I mean, can I cicle on all the table and check the status and if it contains one of the words I delete, if not I go to the next one?

    I know how I would do that in php, with an array and if statements... not sure in VBA.

    thanks!

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: macro - search and delete

    I have 2 sheets - in sheet1, i have put some words starting from A1 to A5. In sheet 2, i have a list of words in column C. I have used this code for this example - you can adapt it to your situation -
    Option Explicit
    Dim foundcell As Range
    Dim lrow As Long
    Dim i As Long
    
    Sub find_values()
    
    lrow = Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lrow
        Set foundcell = Worksheets(2).Range("C:C").Find(what:=Worksheets(1).Range("A" & i).Value)
        If Not foundcell Is Nothing Then foundcell.EntireRow.Delete
    Next i
    
    End Sub
    It will automatically take the words 1 by 1 from sheet1 and locate them in sheet2 and then delete the rows where the words are found.

  6. #6
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: macro - search and delete

    If the words occur many times in the Column then you would need to continue searching the column for any other rows that contain the word. Arlette's code works well if there is a single occurence of the word however if it appears many times then if would not delete all the rows. For interests sake the following will search for ALL occurences of the works, sorts the array with the rows numbers using a bubble sort then deletes the rows. It involves more code simply because it is looking for e.g. the word Apple being in column A 5 times.
    
    Option Explicit
    
    Sub setRNG()
    Dim fCell As Range, lCell As Range, fAdd As String, a
    Dim wdSrch, keyWd As String, k As Long, nCell As Range, x As Long
    Dim First As Integer, last As Integer, i As Integer, j As Integer, temp As Long
    ReDim a(x)
    With Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        Set lCell = .Cells(.Cells.Count)
    End With
    wdSrch = Array("ee", "People", "Orange", "Toast", "Total", "Apple", "Race", "Many", "Pear", "Lost")
    For k = LBound(wdSrch) To UBound(wdSrch)
    keyWd = CStr(wdSrch(k))
    Set fCell = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Find(what:=keyWd, after:=lCell)
    If Not fCell Is Nothing Then
        fAdd = fCell.Address
    End If
    Do Until fCell Is Nothing
    ReDim Preserve a(x)
    a(x) = fCell.Row
    x = x + 1
    Set fCell = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).FindNext(after:=fCell)
        If fCell.Address = fAdd Then
            Exit Do
        End If
    Loop
    Next
        For i = LBound(a) To UBound(a) - 1
            For j = i + 1 To UBound(a)
                If a(i) > a(j) Then
                    temp = a(j)
                    a(j) = a(i)
                    a(i) = temp
                End If
            Next j
        Next i
    For k = UBound(a) To LBound(a) Step -1
    Rows(a(k)).EntireRow.Delete Shift:=xlUp
    Next
    End Sub
    Anthony
    “Confidence never comes from having all the answers; it comes from being open to all the questions.”
    PS: Remember to mark your questions as Solved once you are satisfied and rate the answer(s) questions.”

  7. #7
    Registered User
    Join Date
    01-26-2012
    Location
    dublin
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: macro - search and delete

    Hello!
    Thanks very much for the help!
    As smuzoen said there are more than one occurence for every word... so I tried the second code and it's working!

    Now I would like to have an "half way"... the second code, but I'd like to take the array from a column in a second worksheet of my file called categories.

    I am trying this code but I have an error message "mismatch"...
    I am trying to study how to create arrays and so on... in the meanwhile if you can see the error would be really nice!!
    thanks!!!

    Option Explicit
    Sub CancellaTest()
    
    Dim fCell As Range, lCell As Range, fAdd As String, a
    Dim wdSrch, keyWd As String, k As Long, nCell As Range, x As Long
    Dim First As Integer, last As Integer, i As Integer, j As Integer, temp As Long
    ReDim a(x)
    With Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)
        Set lCell = .Cells(.Cells.Count)
    End With
    
    
    Dim cell As Range
    Dim val As Variant
     
    For Each cell In Worksheets("categories").Range("A1:A10")
         
        val = cell.Value
    Next cell
    
    
    
    For k = LBound(val) To UBound(val)
    keyWd = CStr(val(k))
    Set fCell = Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row).Find(what:=keyWd, after:=lCell)
    If Not fCell Is Nothing Then
        fAdd = fCell.Address
    End If
    Do Until fCell Is Nothing
    ReDim Preserve a(x)
    a(x) = fCell.Row
    x = x + 1
    Set fCell = Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row).FindNext(after:=fCell)
        If fCell.Address = fAdd Then
            Exit Do
        End If
    Loop
    Next
        For i = LBound(a) To UBound(a) - 1
            For j = i + 1 To UBound(a)
                If a(i) > a(j) Then
                    temp = a(j)
                    a(j) = a(i)
                    a(i) = temp
                End If
            Next j
        Next i
    For k = UBound(a) To LBound(a) Step -1
    Rows(a(k)).EntireRow.Delete Shift:=xlUp
    Next
    End Sub
    Last edited by jsabina; 01-27-2012 at 10:32 AM.

  8. #8
    Registered User
    Join Date
    01-26-2012
    Location
    dublin
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: macro - search and delete

    Option Explicit
    Sub CancellaTest()
    
    Dim fCell As Range, lCell As Range, fAdd As String, a
    Dim wdSrch, keyWd As String, k As Long, nCell As Range, x As Long
    Dim First As Integer, last As Integer, i As Integer, j As Integer, temp As Long
    ReDim a(x)
    With Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)
        Set lCell = .Cells(.Cells.Count)
    End With
    
    
    Dim cell As Range
    Dim val As Variant
     
    For Each cell In Worksheets("categories").Range("A1:A10")
         
        val = cell.Value
    Next cell
    
    
    
    For k = LBound(val) To UBound(val)
    keyWd = CStr(val(k))
    Set fCell = Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row).Find(what:=keyWd, after:=lCell)
    If Not fCell Is Nothing Then
        fAdd = fCell.Address
    End If
    Do Until fCell Is Nothing
    ReDim Preserve a(x)
    a(x) = fCell.Row
    x = x + 1
    Set fCell = Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row).FindNext(after:=fCell)
        If fCell.Address = fAdd Then
            Exit Do
        End If
    Loop
    Next
        For i = LBound(a) To UBound(a) - 1
            For j = i + 1 To UBound(a)
                If a(i) > a(j) Then
                    temp = a(j)
                    a(j) = a(i)
                    a(i) = temp
                End If
            Next j
        Next i
    For k = UBound(a) To LBound(a) Step -1
    Rows(a(k)).EntireRow.Delete Shift:=xlUp
    Next
    End Sub

  9. #9
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: macro - search and delete

    I was not sure if you sorted it - If you place the list of words in column A in sheet 2 that you want to search for you can create an array from this - then compare this list of key words against Column A in sheet 1 that is the list that you want to check for keywords
    Option Explicit
    
    Sub setRNG()
    Dim fCell As Range, lCell As Range, fAdd As String, a, wordList As Range
    Dim wdSrch, keyWd As String, k As Long, nCell As Range, x As Long
    Dim First As Integer, last As Integer, i As Integer, j As Integer, temp As Long
    ReDim a(x)
    With Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        Set lCell = .Cells(.Cells.Count)
    End With
    Set wordList = Worksheets("Sheet2").Range("A1:A" & Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row)
    wdSrch = wordList
    For k = LBound(wdSrch, 1) To UBound(wdSrch, 1)
    keyWd = CStr(wdSrch(k, 1))
    Set fCell = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Find(what:=keyWd, after:=lCell)
    If Not fCell Is Nothing Then
        fAdd = fCell.Address
    End If
    Do Until fCell Is Nothing
    ReDim Preserve a(x)
    a(x) = fCell.Row
    x = x + 1
    Set fCell = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).FindNext(after:=fCell)
        If fCell.Address = fAdd Then
            Exit Do
        End If
    Loop
    Next
        For i = LBound(a) To UBound(a) - 1
            For j = i + 1 To UBound(a)
                If a(i) > a(j) Then
                    temp = a(j)
                    a(j) = a(i)
                    a(i) = temp
                End If
            Next j
        Next i
    For k = UBound(a) To LBound(a) Step -1
    Rows(a(k)).EntireRow.Delete Shift:=xlUp
    Next
    End Sub
    If you need any help or other questions just ask.
    Anthony
    “Confidence never comes from having all the answers; it comes from being open to all the questions.”
    PS: Remember to mark your questions as Solved once you are satisfied and rate the answer(s) questions.”
    Last edited by smuzoen; 01-27-2012 at 11:21 AM.

  10. #10
    Registered User
    Join Date
    01-26-2012
    Location
    dublin
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: macro - search and delete

    Thanks Anthony,
    not really because I always get some kind of error
    With your code I whave error on this line
    Rows(a(k)).EntireRow.Delete Shift:=xlUp
    Application defined or object defined error

  11. #11
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: macro - search and delete

    Wow, this seem alot of code for the job.

    ' open workbook with searchwords
    Application.Workbooks.Open ("Your file path")
    
    ' create array holding values from that workbook
    arr = Workbook("Your workbook Name").Sheets(2).Range("A1:A50").value
    
    ' Close workbook
    Workbook("Your workbook Name").Close
    
    ' Begin loop
    For i = 1 to ubound(arr)
        Do
             Sheets(1).Columns(1).Find(arr(i), , xlValues,xlWhole).EntireRow.Delete
        Loop until err.number <> 0
    next i
    No more is needed and even less if you have your validation list in the same workbook or a txt file
    Please take time to read the forum rules

  12. #12
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: macro - search and delete

    Try the attached workbook - if you can get Steffens code to work by all means shorter code is better. Anyway see attached workbook.
    Attached Files Attached Files
    Last edited by smuzoen; 01-27-2012 at 12:07 PM.

+ 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