+ Reply to Thread
Results 1 to 11 of 11

Macro to delete row if cell <> value...

Hybrid View

  1. #1
    Registered User
    Join Date
    10-14-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007
    Posts
    67

    Macro to delete row if cell <> value...

    Hey guys,

    I need a short and simple macro that deletes any row that contains "*EDS*" in column A...this is the code I am using but it doesnt seem to be working correctly, it hangs up on line 2...

        
    Application.ScreenUpdating = False
        With Sheets("IDs in IBM OU").Range("A1", Range("A" & Rows.Count).End(xlUp))
            .AutoFilter Field:=1, Criteria1:="*EDS*"
            .Offset(1).EntireRow.Delete
            .AutoFilter
        End With
        Application.ScreenUpdating = True

  2. #2
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Macro to delete row if cell <> value...

    Do the cells in column A only contain either *EDS* or no *EDS*? or are you trying to search through a paragraph of text for each cell in column A, and only delete if within that paragraph of text *EDS* shows up?

    Jason

  3. #3
    Registered User
    Join Date
    10-14-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Macro to delete row if cell <> value...

    No, the cells will have various information....EDS will just be in the middle of a lot of text.

    Example:

    HTML Code: 

  4. #4
    Registered User
    Join Date
    06-07-2012
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010, Excel 2007
    Posts
    57

    Re: Macro to delete row if cell <> value...

    FinalRow = Cells(Rows.Count, 56).End(xlUp).Row
    For i = FinalRow To 1 Step -1

    If Cells(i, 1).Value = "*EDS* Then
    Cells(i,1).Delete
    End If

    Next i

  5. #5
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Macro to delete row if cell <> value...

    Hi,

    try changing
      With Sheets("IDs in IBM OU").Range("A1", Range("A" & Rows.Count).End(xlUp))
    to
        dim lastrow as integer
        lastrow = Sheets("IDs in IBM OU").UsedRange.Rows.Count
        With Sheets("IDs in IBM OU").Range("A1:A" & lastrow)
    OR replace entirely with this code ..

    Sub find()
    
    Dim a As Variant, r As Range, lastrow As Integer
    lastrow = Sheets("IDs in IBM OU").UsedRange.Rows.Count
    
    For Each r In Sheets("IDs in IBM OU").Range("A1:A" & lastrow)
    a = Application.Match("*EDS*", Columns(1), 0)
    
    If IsError(a) Then
    Else
    Cells(a, 1).EntireRow.Delete
    End If
    
    Next r
    
    End Sub
    Last edited by jasondu; 06-22-2012 at 12:03 PM.

  6. #6
    Registered User
    Join Date
    10-14-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Macro to delete row if cell <> value...

    Jason, that does work - however I had to run it 3 times in order for it to delete all the EDS rows...
    For some reason it is not catching them all in one run...i am going to try it a couple more times

  7. #7
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Macro to delete row if cell <> value...

    hmm it shoudlve deleted all (it worked for me), but anyways see modified post I fixed your code for a even simpler solution

  8. #8
    Registered User
    Join Date
    10-14-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Macro to delete row if cell <> value...

    I appreciate all your help with this

  9. #9
    Registered User
    Join Date
    10-14-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Macro to delete row if cell <> value...

    I managed to work around it and add it 3 ways...not the best way to do it, but it works lol -

    Also one more question, what is the code change for application.match if say I didnt want it to match "*EDS*". If that makes sense...

    So logic would be: If it does not equal "*EDS*" then delete entire row.

  10. #10
    Registered User
    Join Date
    05-18-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003
    Posts
    59

    Re: Macro to delete row if cell <> value...

    Hey Tshelt,

    Did you try changing
      With Sheets("IDs in IBM OU").Range("A1", Range("A" & Rows.Count).End(xlUp))
    to
        dim lastrow as integer
        lastrow = Sheets("IDs in IBM OU").UsedRange.Rows.Count
        With Sheets("IDs in IBM OU").Range("A1:A" & lastrow)
    Also .. do you have any blanks in column A? how many rows do you have? I tried my code and it worked for up to 300 lines with varying *EDS* in random text can you upload your sheet?
    Last edited by jasondu; 06-22-2012 at 12:26 PM.

  11. #11
    Registered User
    Join Date
    10-14-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Macro to delete row if cell <> value...

    I cannot upload my sheet as it has info on it I dont want out...

    The sheet has 365 rows but it varies weekly and no blanks.

+ 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